ios - NSDictionary 为 NSNumber 返回 nil

标签 ios nsdictionary gamekit nsnumber

我正在使用 gamekit 在 nsdictionary 中发送一个 nsnumber。当我 NSlog 字典时,数字没问题,但当我尝试获取 NSNumber 时,它总是(空)。

NSMutableDictionary *dict = [NSKeyedUnarchiver  unarchiveObjectWithData:data];
NSNumber *receivedHostNum = (NSNumber*)[dict objectForKey:@"h"];
if (gameStarted == NO){

    NSLog(@"%@",dict);
    NSLog(@"My Num:%i",[hostNum intValue]);
    NSLog(@"%@",receivedHostNum);
    NSLog(@"Recieved Num:%i",[receivedHostNum intValue]);

    if ([hostNum intValue]>[receivedHostNum intValue])
        NSLog(@"You are host");
    else
        NSLog(@"You are client");
}

这是日志:

2012-08-21 14:34:06.067 PeerPicker[6667:c07] {
    "-589267889" = h;
}
2012-08-21 14:34:06.068 PeerPicker[6667:c07] My Num:-2142897577
2012-08-21 14:34:06.068 PeerPicker[6667:c07] (null)
2012-08-21 14:34:06.069 PeerPicker[6667:c07] Recieved Num:0
2012-08-21 14:34:06.069 PeerPicker[6667:c07] You are client

最佳答案

我认为您在字典中输入的号码有误。您的日志显示:

2012-08-21 14:34:06.067 PeerPicker[6667:c07] {
    "-589267889" = h;
}

应该是:

2012-08-21 14:34:06.067 PeerPicker[6667:c07] {
    "h" = -589267889;
}

您在构造数组或存储数组时将键和对象颠倒了。

关于ios - NSDictionary 为 NSNumber 返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12056122/

相关文章:

objective-c - ASIHTTPRequest 和 NSURLRequest 不同的结果

iphone - 使用 for 循环将对象添加到字典

ios - 将事件监听器添加到基于回合的 ios 游戏时发出警告

ios - GameKit、GKGameSession 如何将玩家添加到 session 中并发送数据?

ios - 在React Native中出现“未定义不是对象(评估StyleSheet.create)”错误

ios - 如何找到瞳孔中心和眼镜框边缘之间的距离

ios - 使用 UIBezierPath 沿弧线移动 Sprite

objective-c - NSDictionary setValue :forKey: -- getting "this class is not key value coding-compliant for the key"

ios - NSCFString 0x2749a0 valueForUndefinedKey 此类对于键 push_type 不符合键值编码

ios - 使用 GameKit 将 UISlider 的移动值从一台设备发送到另一台设备