iOS应用程序崩溃并出现错误:NSCFNumber stringByReplacingOccurrencesOfString:withString:无法识别的选择器发送到实例

时间:2014-01-08 00:40:44

标签: ios objective-c unrecognized-selector

我们的iOS应用终止时出现[__NSCFNumber stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance 0x1755e090错误。

我们正在测试iOS 5的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

1 个答案:

答案 0 :(得分:4)

当你使用时:

[[[[inDictionary objectForKey:key]
               stringByReplacingOccurrencesOfString:@"\\" withString:@"\\\\"]
                stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""]
                stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"]];

您假设objectForKey将返回NSString个对象。但是,在您的应用崩溃的情况下,返回的对象实际上是NSNumber

您应该使用isKindOfClass:来确定对象类型,或者使用stringValue上的NSNumber来获取对象的字符串表示。