PDA

View Full Version : NSMutableArray Question




CeiJay
Jul 18, 2010, 12:22 PM
How would one use this Array...

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...

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

or

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



dejo
Jul 18, 2010, 12:59 PM
Are you trying to figure out how to reference a single element/object from the array using, say, an index number?

CeiJay
Jul 18, 2010, 01:04 PM
Are you trying to figure out how to reference a single element/object from the array using, say, an index number?

Hi Again Dejo

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

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...

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

or this...

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.

chown33
Jul 18, 2010, 01:40 PM
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.

CeiJay
Jul 18, 2010, 01:48 PM
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.

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:@"%@" ofType:@"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:@"%@" ofType:@"mp4"];

Thoughts?

chown33
Jul 18, 2010, 02:45 PM
NSString *path = [[NSBundle mainBundle] pathForResource:@"%@" ofType:@"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:@"%@" ofType:@"mp4"];

Thoughts?
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:
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.

CeiJay
Jul 18, 2010, 02:48 PM
Thank you for all you help Chown - I will work on it...

CeiJay
Jul 18, 2010, 06:37 PM
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:
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.

Thank you for your help.

The solution was the following...

NSInteger row = indexPath.row;
NSString *rowTitle = [filmMovie objectAtIndex:row];

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