插入 BOOL 的 Objective-C 字典
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2108042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Objective-C dictionary inserting a BOOL
提问by Infinite
OK, I'm a little confused. It's probably just a triviality.
好吧,我有点困惑。这可能只是小事一桩。
I've got a function which looks something like this:
我有一个看起来像这样的函数:
- (void)getNumbersForNews:(BOOL)news andMails:(BOOL)mails {
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
[parameters setValue:news forKey:@"getNews"];
[parameters setValue:mails forKey:@"getMails"];...}
It doesn't matter whether I use setValue:forKey:or setObject:ForKey:, I'm always getting a warning:
无论我使用setValue:forKey:或setObject:ForKey:,我总是收到警告:
"Passing argument 1 of set... makes pointer from integer without a cast"...
“传递 set 的参数 1...使指针从整数而不进行强制转换”...
How on earth do I insert a bool into a dictionary?
我到底如何在字典中插入布尔值?
回答by Steve Harrison
Values in an NSDictionarymust be objects. To solve this problem, wrap the booleans in NSNumberobjects:
an 中的值NSDictionary必须是对象。要解决此问题,请将布尔值包装在NSNumber对象中:
[parameters setValue:[NSNumber numberWithBool:news] forKey:@"news"];
[parameters setValue:[NSNumber numberWithBool:mails] forKey:@"mails"];
回答by Vladimir
Objective-C containers can store only Objective-C objects so you need to wrap you BOOL in some object. You can create a NSNumber object with [NSNumber numberWithBool]and store the result.
Later you can get your boolean value back using NSNumber's -boolValue.
Objective-C 容器只能存储 Objective-C 对象,因此您需要将 BOOL 包装在某个对象中。您可以创建一个 NSNumber 对象[NSNumber numberWithBool]并存储结果。
稍后您可以使用 NSNumber 的-boolValue.
回答by AlBlue
A BOOL is not an object - it's a synonym for an int and has 0 or 1 as its values. As a result, it's not going to be put in an object-containing structure.
BOOL 不是对象 - 它是 int 的同义词,其值是 0 或 1。因此,它不会被放入包含对象的结构中。
You can use NSNumber to create an object wrapper for any of the integer types; there's a constructor [NSNumber numberWithBool:]that you can invoke to get an object, and then use that. Similarly, you can use that to get the object back again: [obj boolValue].
您可以使用 NSNumber 为任何整数类型创建对象包装器;有一个构造函数[NSNumber numberWithBool:],你可以调用它来获取一个对象,然后使用它。同样的,你可以用它来重新获取对象回来:[obj boolValue]。
回答by Hatchmaster J
Modern code for reference:
现代代码供参考:
parameters[@"getNews"] = @(news);
回答by Eimantas
You can insert @"YES"or @"NO"string objects and Cocoa will cast it to bool once you read them back.
您可以插入@"YES"或@"NO"字符串对象,一旦您读回它们,Cocoa 会将其转换为 bool。
Otherwise I'd suggest creating dictionary using factory method like dictionaryWithObjectsAndKeys:.
否则我建议使用像dictionaryWithObjectsAndKeys:.
回答by pechar
Seeing @Steve Harrison's answer I do have one comment. For some reason this doesn't work with passing object properties like for e.g.
看到@Steve Harrison 的回答,我确实有一条评论。出于某种原因,这不适用于传递对象属性,例如
[parameters setValue:[NSNumber numberWithBool:myObject.hasNews] forKey:@"news"];
This sets the newskey to null in the parameter NSDictionary(for some reason can't really understand why)
这将news参数中的键设置为 null NSDictionary(出于某种原因无法真正理解为什么)
My only solution was to use @Eimantas's way as follows:
我唯一的解决方案是使用@Eimantas 的方式如下:
[parameters setValue:[NSNumber numberWithBool:myObject.hasNews ? @"YES" : @"NO"] forKey:@"news"];
This worked flawlessly. Don't ask me why passing the BOOLdirectly doesn't work but at least I found a solution. Any ideas?
这完美无缺。不要问我为什么BOOL直接传递不起作用,但至少我找到了解决方案。有任何想法吗?

