NSArray项目为NSString

1 iphone nsstring nsarray

在一个方法中,我有以下代码:

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

当我编译代码时,它的编译没有任何错误和警告.但是在运行时(在带有断点的调试模式下)它在NSLog语句中崩溃.我遇到的麻烦是在获取"[notificationDetails objectAtIndex:2];"时 这是什么解决方案.

Tec*_*Zen 6

使用...

NSLog(tempString1);
Run Code Online (Sandbox Code Playgroud)

......很危险 虽然看起来NSLog会接受任何字符串作为其唯一参数,但实际上调用的实际函数的定义清楚地表明第一个参数在内部被解析为格式字符串.

void NSLogv (
   NSString *format,
   va_list args
);
Run Code Online (Sandbox Code Playgroud)

这意味着字符串中具有格式含义即"%"的任何字符都将被视为格式化而非字符.因此,如果您尝试传递字符串,"销售40%的折扣"NSLog将查找参数并在找不到它们时崩溃.