iPhone SDK: Copying Array
I have an array :
//current array
NSMutableArray *arrayCurrentArray;
//and index indexCurrentPage = 1;
the array needs to be initialized at run-time like this:
arrayCurrentArray =
[[NSMutableArray alloc]
initWithArray:arrayBrotherPirateEnglish
copyI开发者_StackOverflowtems:YES];
this is how the array is initialized:
arrayBrotherPirateEnglish = [NSArray arrayWithObjects:@"OBTP English full with text", @"OBTP English p1", @"OBTP English p2", @"OBTP English p3", @"OBTP English p4", @"OBTP English p5", @"OBTP English p6",
@"OBTP English p7", @"OBTP English p8", @"OBTP English p9", @"OBTP English p10", @"OBTP English p11", nil];
I then have code like this:
indexCurrentPage++;
if(indexCurrentPage <= [arrayCurrentArray count]) {
//next page to play
strFileName = [arrayCurrentArray objectAtIndex:indexCurrentPage];
NSLog(@"next page filename %s ", strFileName);
//set the player
NSURL *url = [NSURL URLWithString:strFileName];
[player setContentURL:url];
//play
[player play];
}
My problem is that I cannot access items within the array.
This is the error I am getting:
2010-07-26 12:57:18.888 BookReaderJuly23[49638:207] next page filename ‡ûú»
2010-07-26 12:57:18.890 BookReaderJuly23[49638:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:isDirectory:]: nil string parameter'
2010-07-26 12:57:18.892 BookReaderJuly23[49638:207] Stack: (
Any insight appreciated.
Your array contains NSString objects, not C style "strings". You should use the "%@" format specifier instead of "%s" to log them:
NSLog(@"next page filename %@", strFileName);
You don't show how strFileName is declared, but it should be like this:
NSString *strFileName;
You also have an off-by-one error, since NSArrays use 0-based indexing. Start your index at 0 and your loop condition should be:
if(indexCurrentPage < [arrayCurrentArray count])
rather than
if(indexCurrentPage <= [arrayCurrentArray count])
Then, if you want to create a file URL, use the correct class method (+fileURLWithPath:):
NSURL *url = [NSURL fileURLWithPath:strFileName];
Those are the obvious problems and should help you make some progress. You might also include the first few frames of your stack trace next time as it likely has some good info.
This line:
NSLog(@"next page filename %s ", strFileName);
is incorrect. strFileName
is an NSString; you need to use the "%@" format specifier (instead of "%s" to print it out.
Check the documentation on NSURL for the method you are using.
http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/Reference/Reference.html#//apple_ref/doc/uid/20000301-BAJBBDIB
Focus on this part The string with which to initialize the NSURL object. Must conform to RFC 2396. This method parses URLString according to RFCs 1738 and 1808.
Based on what you have here it looks like you aren't conforming to RFC 2396
精彩评论