valueForKey 只返回内存地址而不是实际值
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 时,该值始终是内存地址。我做错了什么?
NSDictionary *topic = [spaces objectAtIndex:i];
NSInteger topicid = [topic valueForKey:@"TOPICID"];
when I run this and print out topic I get the following:
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"}
)}
However, when I look at topicid, the value is always a memory address. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许该值实际上是一个 NSNumber。你可以使用以下方法得到这个:
Maybe the value is actually an NSNumber. You would get this using:
字典只能包含对象,不能包含基元。那里存储的任何内容(正如 Xetius 所建议的)可能都以某种形式包装。看起来 TOPICID 是一个 NSString。
Dictionaries can only contain objects, not primitives. Whatever is stored in there (as Xetius suggested) is probably wrapped in some form. It would appear that TOPICID is an NSString.
在数组输出中,您可以看到 topicID 是一个字符串值(NSString)。因此,您可能希望按照 Xetius 的描述来设置它 - 但不是因为它是一个 NSNumber,因为 NSString 有一个“intValue”方法。
In the array output, you can see topicID is a string value (NSString). So, you would want to set it as Xetius describes - but not because it is an NSNumber, because NSString has an "intValue" method.