从 NSDictionary 枚举 BOOL
我有一个 .plist 文件已加载到我的 Xcode 项目中。我在测试时已成功将其放入我的iPhone的文档目录中。当我将内容转储到 NSMutableDictionary 并尝试枚举它时,我遇到 EXC_BAD_ACCESS 崩溃。它们的键都有关联的 BOOL 作为它们的值。我做错了什么?
我现在的代码:
for (id key in achDict) {
NSLog(@"Achievement:%@ done:%@", key, [[achDict objectForKey:key] boolValue]);
}
崩溃时总是返回 EXC_BAD_ACCESS。
I have a .plist file that is loaded into my Xcode project. I have successfully put it in the documents directory of my iPhone while testing it. When I dump the contents into an NSMutableDictionary, and try to enumerate it, I get EXC_BAD_ACCESS crashes. They keys all have BOOLs associated as their values. What am I doing wrong?
My code now:
for (id key in achDict) {
NSLog(@"Achievement:%@ done:%@", key, [[achDict objectForKey:key] boolValue]);
}
This always returns EXC_BAD_ACCESS in a crash.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 NSLog 需要两个对象,但您向它传递了一个字符串“key”和一个整数。 Bool 值不是对象,它返回一个整数值(0 表示 False,1 表示 True)。
%@
用于 Objective C 对象。而是使用%d
来获取整数值,例如 C 布尔值。将 NSLog 语句更改为:
Apple 的 字符串编程指南 有一个关于字符串修饰符的有用部分
Your NSLog is expecting two objects but you are passing it a string 'key' and an Integer. A Bool Value is not an Object, it returns an Integer value (0 for False and 1 for True).
%@
is for Objective C Objects. Instead use%d
to get Integer Values such as C Booleans.Change your NSLog statement to:
Apple's String Programming Guide has a useful section on String Modifiers