objective-c 如何向 NSDictionary 添加布尔值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/903906/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:36:16  来源:igfitidea点击:

How can I add a boolean value to a NSDictionary?

objective-ciphonecocoa-touchuikit

提问by Thanks

Well, for integers I would use NSNumber. But YES and NO aren't objects, I guess. A.f.a.i.k. I can only add objects to an NSDictionary, right?

好吧,对于整数,我会使用NSNumber. 但是 YES 和 NO 不是对象,我猜。Afaik 我只能向 an 添加对象NSDictionary,对吗?

I couldn't find any wrapper class for booleans. Is there any?

我找不到任何布尔值的包装类。有没有?

回答by harms

You use NSNumber.

你使用 NSNumber。

It has init... and number... methods that take booleans, just as it does integers and so on.

它有 init... 和 number... 方法接受布尔值,就像它处理整数等一样。

From the NSNumber class reference:

NSNumber 类参考

// Creates and returns an NSNumber object containing a 
// given value, treating it as a BOOL.
+ (NSNumber *)numberWithBool:(BOOL)value

and:

和:

// Returns an NSNumber object initialized to contain a
// given value, treated as a BOOL.
- (id)initWithBool:(BOOL)value

and:

和:

// Returns the receiver's value as a BOOL.
- (BOOL)boolValue

回答by Brian

The new syntax since Apple LLVM Compiler 4.0

新语法自 Apple LLVM Compiler 4.0

dictionary[@"key1"] = @(boolValue);
dictionary[@"key2"] = @YES;

The syntax converts BOOLto NSNumber, which is acceptable to NSDictionary.

语法转换BOOLNSNumber,这是可以接受的NSDictionary

回答by sabalaba

If you are declaring it as a literal and you are using clang v3.1 or above, you should use @NO / @YES if you are declaring it as a literal. E.g.

如果您将其声明为文字并且您使用的是 clang v3.1 或更高版本,则您应该使用 @NO / @YES 如果您将其声明为文字。例如

NSMutableDictionary* foo = [@{ @"key": @NO } mutableCopy];
foo[@"bar"] = @YES;

For more info on that:

有关更多信息:

http://clang.llvm.org/docs/ObjectiveCLiterals.html

http://clang.llvm.org/docs/ObjectiveCLiterals.html

回答by Vojta

As jcampbell1pointed out, now you can use literal syntax for NSNumbers:

正如jcampbell1指出的,现在您可以对 NSNumbers 使用文字语法:

NSDictionary *data = @{
                      // when you always pass same value
                      @"someKey" : @YES
                      // if you want to pass some boolean variable
                      @"anotherKey" : @(someVariable)
                      };

回答by NSPratik

Try this:

尝试这个:

NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
[dic setObject:[NSNumber numberWithBool:TRUE]  forKey:@"Pratik"];
[dic setObject:[NSNumber numberWithBool:FALSE] forKey:@"Sachin"];

if ([dic[@"Pratik"] boolValue])
{
    NSLog(@"Boolean is TRUE for 'Pratik'");
}
else
{
    NSLog(@"Boolean is FALSE for 'Pratik'");
}

if ([dic[@"Sachin"] boolValue])
{
    NSLog(@"Boolean is TRUE for 'Sachin'");
}
else
{
    NSLog(@"Boolean is FALSE for 'Sachin'");
}

The output will be as following:

输出如下:

Boolean is TRUEfor 'Pratik'

' Pratik' 的布尔值为TRUE

Boolean is FALSEfor 'Sachin'

' Sachin' 的布尔值为FALSE