views:

388

answers:

4

In a method, I'm having following code:

NSString* tempString = (NSString*)arg;
NSArray* notificationDetails = [tempString componentsSeparatedByString:@"##"];
NSString* tempString1 = [notificationDetails objectAtIndex:2];
.
.
.
.
.
. 
NSLog(tempString1);

When I compile the code, its compiling without any error and warning. But while running (in debug mode with breakpoints) its crashing at NSLog statement. The trouble I'm seeing is while getting "[notificationDetails objectAtIndex:2];" What's the solution for this.

+1  A: 

Use

NSLog(@"%@", tempString1);

CiNN
If tempString1 is a (valid) string, it's also a valid format string.(Though I also prefer the explicit version with a format string)
Chris
If your string really is a format string, but you don't pass the necessary arguments, your app might crash. Try NSLog(@"%@");
Daniel Hepper
A: 

How many elements does notificationDetails contain? Use the following code to find out: NSLog(@"%d elements", [notificationDetails count]);

Is arg a string at all? NSLog(@"Class of arg is %@", [arg class]);

Chris
+6  A: 

Using...

NSLog(tempString1);

... is dangerous. Although it looks like NSLog will accept any string as its sole parameter, in fact the definition of the actual function called makes it clear that the first parameter is parsed internally as a format string.

void NSLogv (
   NSString *format,
   va_list args
);

This means that any characters in the string that have format meaning i.e. "%" will be treated parsed as formatting, not characters. So if you try to pass the string, "sale 40% off" NSLog will look for arguments and crash when it does not find them.

TechZen
A: 

An additional possibility, depending on the length of the ellipsis: componentsSeparatedByString: returns an autoreleased array. But I'd guess insufficient components.

David Dunham