NSDictionary为NSNumber返回nil

时间:2012-08-21 13:37:51

标签: ios nsdictionary gamekit nsnumber

我正在使用gamekit在nsdictionary中发送nsnumber。当我对字典进行NSlog时,数字是可以的,但是当我尝试获取NSNumber时,它总是(null)。

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

1 个答案:

答案 0 :(得分:3)

我认为你的错误方式已将你的号码放在字典中了。您的日志显示:

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

应该是:

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

在构建阵列或存储阵列时,您已经反转了键和对象。