NSDictionary 的 NSCFNumber escapedString 错误

NSCFNumber escapedString Error with a NSDictionary

所以我在创建 NSDictionary:

时遇到了这个错误
DLog(@"hiliteID: %@ | regionID: %@", hiliteID, regionID);

if ([hiliteID isKindOfClass:[NSNumber class]]) {
    DLog(@"hititeID is a number");
}
if ([hiliteID isKindOfClass:[NSString class]]) {
    DLog(@"hiliteID is a string");
}
if ([regionID isKindOfClass:[NSNumber class]]) {
    DLog(@"regionID is a number");
}
if ([regionID isKindOfClass:[NSString class]]) {
    DLog(@"regionID is a string");
}

NSDictionary *regionDictionary = [NSDictionary dictionaryWithObject:regionID forKey:hiliteID];
DLog(@"regionDictionary: %@", regionDictionary);

id result = [self.serverCall XMLRPCCall:kSaveHilitedObjects withObjects:@[self.mapContext, regionDictionary]];

记录的内容:

DEBUG | hiliteID: 160399 | regionID: 950
DEBUG | hititeID is a number
DEBUG | regionID is a number
DEBUG | regionDictionary: {
    160399 = 950;
}
DEBUG -[__NSCFNumber escapedString]: unrecognized selector sent to instance 0x7947f5d0
DEBUG *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber escapedString]: unrecognized selector sent to instance 0x7947f5d0'

0x7947f5d0 的值为 160399 所以它是 hiliteID.

hiliteID是我们服务器的返回值,设置为NSString。我将其转换为 NSNumber:

NSArray *hiliteIDs = [result allKeys];

if ([[hiliteIDs firstObject] isKindOfClass:[NSString class]]) {
    return [NSNumber numberWithInteger:[[hiliteIDs firstObject] integerValue]];
}
else if ([[hiliteIDs firstObject] isKindOfClass:[NSNumber class]]) {
    return [hiliteIDs firstObject];
}

据我所知,我在这里做的事情没有问题。

行:

id result = [self.serverCall XMLRPCCall:kSaveHilitedObjects withObjects:@[self.mapContext, regionDictionary]];

我已经在代码中使用这个 class 几十次了,从来没有出现过问题。

可能导致错误的原因是什么?

reason: '-[__NSCFNumber escapedString]: unrecognized selector sent to instance 0x7947f5d0'

有人试图从 NSNumber class 调用 -escapedString 方法,所以问题似乎是您在需要 NSString 时传递了 NSNumber 参数。尝试在 NSDictionary 中仅使用 NSString 值。

无论什么 XMLRPCCall:withObjects: 都在调用无效方法。我建议将此方法的所有输入都输入 NSStrings,这样调用就不会在内部调用 NSNumbersNSNumber 无法响应的方法。

NSString *regionIDString = [regionID stringValue];
NSString *hiliteIDString = [hiliteID stringValue];
NSDictionary *regionDictionary = [NSDictionary dictionaryWithObject:regionIDString forKey:hiliteIDString];