valueForKey只返回内存地址而不是实际值

时间:2009-10-15 14:16:12

标签: iphone objective-c nsdictionary nsinteger memory-address

    NSDictionary *topic = [spaces objectAtIndex:i];
    NSInteger topicid = [topic valueForKey:@"TOPICID"];

当我运行并打印出主题时,我得到以下内容:

Printing description of topic:
<CFDictionary 0xdb2a70 [0x30307a00]>{type = mutable, count = 2, capacity = 12, pairs = (
    10 : <CFString 0xdb5300 [0x30307a00]>{contents = "TOPICID"} = 29
    12 : <CFString 0xdb53a0 [0x30307a00]>{contents = "TOPICNAME"} = <CFString 0xdb5360 [0x30307a00]>{contents = "zzzzzzzz"}
)}

但是,当我查看topicid时,值始终是内存地址。我做错了什么?

3 个答案:

答案 0 :(得分:7)

也许这个值实际上是一个NSNumber。你会得到这个:

NSInteger topicid = [[topic objectForKey:@"TOPICID"] intValue];

答案 1 :(得分:0)

字典只能包含对象,而不能包含基元。无论存储在那里(正如Xetius建议的那样)可能以某种形式包装。看来TOPICID是一个NSString。

答案 2 :(得分:0)

在数组输出中,您可以看到topicID是一个字符串值(NSString)。因此,您可能希望将其设置为Xetius描述 - 但不是因为它是NSNumber,因为NSString具有“intValue”方法。