NSMutableArray Question

Discussion in 'iPhone/iPad Programming' started by CeiJay, Jul 18, 2010.

  1. macrumors newbie

    CeiJay

    Joined:
    Jul 17, 2010
    #1
    How would one use this Array...

    Code:
    if (filmMovie == nil)
        {
            NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:
                                     @"Mov1.mp4", 
    								 @"Mov2.mp4", 
    								 @"Mov1.mp4", 
    								 @"Mov2.mp4", 
    								 @"Mov1.mp4", 
    								 @"Mov2.mp4", 
                                     @"Mov1.mp4", 
    								 @"Mov2.mp4", 
    								 @"Mov1.mp4", 
    								 @"Mov2.mp4", nil];
            self.filmMovie = array;
            [array release];        
        }
    to populate this...

    Code:
    NSString *path = [[NSBundle mainBundle] pathForResource:@"INFO FROM ARRAY" ofType:@"mp4"];
    or

    Code:
    NSString *path = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"INFO FROM ARRAY"];
     
  2. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Are you trying to figure out how to reference a single element/object from the array using, say, an index number?
     
  3. thread starter macrumors newbie

    CeiJay

    Joined:
    Jul 17, 2010
    #3
    Hi Again Dejo

    I managed to get the following to load an audio file from an Array without a problem.

    Code:
    NSUInteger row = [indexPath row];
        NSString *rowTitle = [filmSound objectAtIndex:row];
    
    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] resourcePath], rowTitle]];
    	NSError *error;
    	AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    
    yet with this...

    Code:
    NSString *path = [[NSBundle mainBundle] pathForResource:@"INFO FROM ARRAY" ofType:@"mp4"];
    or this...

    Code:
    NSString *path = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"INFO FROM ARRAY"];
    I am unable to do the exact same thing. Using the Audio example does not work as NSString*path is required.
     
  4. macrumors 603

    Joined:
    Aug 9, 2009
    #4
    Show real code. The literal string @"INFO FROM ARRAY" can't possibly work.

    Also, pathForResource expects resource names to NOT have an extension if the ofType parameter is non-nil. Your array of strings DOES have ".mp4" extensions on every string. So either remove the extensions from the array strings, or pass nil instead of "mp4". Refer to the NSBundle reference doc, in the section on pathForResource methods.
     
  5. thread starter macrumors newbie

    CeiJay

    Joined:
    Jul 17, 2010
    #5
    Hi Chown

    Thank you for your input. I am happy with the inType and removing from the Array and the Nil pass. Refined Question.

    NSString *path = [[NSBundle mainBundle] pathForResource:mad:"%@" ofType:mad:"mp4"];

    %@ need to be whatever is in the Array. One example that seems not to work because it changes NSString *path to NSURL *path.

    NSURL *path = [NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:rowTitle ofType:nil]];

    --

    How to load info from the Array into here... making sure to keep using NSString *path =

    NSString *path = [[NSBundle mainBundle] pathForResource:mad:"%@" ofType:mad:"mp4"];

    Thoughts?
     
  6. macrumors 603

    Joined:
    Aug 9, 2009
    #6
    If that's your actual code, it can't possibly work. You can't just throw in a formatting string @"%@" for any method. Only a small number of methods accept formatting strings. pathForResource is not one of them.

    You've already posted code that retrieves an indexed string from the array. It's this:
    Code:
    NSUInteger row = [indexPath row];
        NSString *rowTitle = [filmSound objectAtIndex:row];
    
    If you can't figure out how to proceed, then you should think about what's in the string rowTitle, and how that relates to the pathForResource string. If it's still not clear, you need to go back and review the basics.

    You should also take a little time and mentally step through what value is in each variable. Use a pencil and paper if you can't keep it all in your head. If you don't understand what values are in which variables, and how those values relate to what you're trying to do, you can't possibly write code effectively. You need to think it through and understand the code.
     
  7. thread starter macrumors newbie

    CeiJay

    Joined:
    Jul 17, 2010
    #7
    Thank you for all you help Chown - I will work on it...
     
  8. thread starter macrumors newbie

    CeiJay

    Joined:
    Jul 17, 2010
    #8
    Thank you for your help.

    The solution was the following...

    Code:
    NSInteger row = indexPath.row;
    	NSString *rowTitle = [filmMovie objectAtIndex:row];
    
    		NSString *path = [[NSBundle mainBundle] pathForResource:rowTitle ofType:nil];
     

Share This Page