![](/img/trans.png)
[英]My iPhone app is crashing and showing [__NSCFDictionary stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance
[英]iOS app crashing with error: NSCFNumber stringByReplacingOccurrencesOfString:withString: unrecognized selector sent to instance
我們的iOS應用終止於錯誤[__NSCFNumber stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance 0x1755e090
。
我們正在使用iOS 7的iPhone 5進行測試。
它在以下行崩潰: [self parseDictionary:notificationMessage intoJSON:jsonStr]
。
包含此行的方法:
- (void)notificationReceived {
NSLog(@"Notification received");
if (notificationMessage && self.callback)
{
NSMutableString *jsonStr = [NSMutableString stringWithString:@"{"];
[self parseDictionary:notificationMessage intoJSON:jsonStr];
if (isInline)
{
[jsonStr appendFormat:@"foreground:\"%d\"", 1];
isInline = NO;
}
else
[jsonStr appendFormat:@"foreground:\"%d\"", 0];
[jsonStr appendString:@"}"];
NSLog(@"Msg: %@", jsonStr);
NSString * jsCallBack = [NSString stringWithFormat:@"%@(%@);", self.callback, jsonStr];
[self.webView stringByEvaluatingJavaScriptFromString:jsCallBack];
self.notificationMessage = nil;
}
}
-(void)parseDictionary:(NSDictionary *)inDictionary intoJSON:(NSMutableString *)jsonString
{
NSArray *keys = [inDictionary allKeys];
NSString *key;
for (key in keys)
{
id thisObject = [inDictionary objectForKey:key];
if ([thisObject isKindOfClass:[NSDictionary class]])
[self parseDictionary:thisObject intoJSON:jsonString];
else
[jsonString appendFormat:@"\"%@\":\"%@\",",
key,
[[[[inDictionary objectForKey:key]
stringByReplacingOccurrencesOfString:@"\\" withString:@"\\\\"]
stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""]
stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"]];
}
}
堆棧跟蹤(以及一條調試行以顯示變量值):
2014-01-07 16:32:36.980 Wopple[195:60b] Notification received
Printing description of self->notificationMessage:
{
"_" = "gHf8EeO3_ZDiugJkgA";
aps = {
alert = "hi foo bar right";
badge = 3;
};
}
2014-01-07 16:33:22.774 Wopple[195:60b] -[__NSCFNumber stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance 0x1755e090
2014-01-07 16:33:22.776 Wopple[195:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance 0x1755e090'
*** First throw call stack:
(0x308c3e83 0x3ac246c7 0x308c77b7 0x308c60af 0x30814dc8 0x88da5 0x88d1f 0x88915 0x8757b 0x3335ec93 0x3335f75d 0x340d0b37 0x3088e777 0x3088e713 0x3088cedf 0x307f7471 0x307f7253 0x3552b2eb 0x330ac845 0x7f5d3 0x3b11dab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
使用時:
[[[[inDictionary objectForKey:key]
stringByReplacingOccurrencesOfString:@"\\" withString:@"\\\\"]
stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""]
stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"]];
您假設objectForKey
將返回一個NSString
對象。 但是,在您的應用崩潰的情況下,返回的對象實際上是一個NSNumber
。
您應該使用isKindOfClass:
確定對象類型,或者在NSNumber
上使用stringValue
獲取對象的字符串表示形式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.