【问题标题】:How can I add a boolean value to a NSDictionary?如何向 NSDictionary 添加布尔值?
【发布时间】:2009-05-24 14:41:21
【问题描述】:

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

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

【问题讨论】:

  • 以防万一,A.f.a.i.k.据我所知

标签: objective-c iphone cocoa-touch uikit


【解决方案1】:

你使用 NSNumber。

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

来自NSNumber class reference

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

和:

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

和:

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

【讨论】:

  • 太棒了!我猜它在内部将 bool 存储为 0 / 1?
  • @harms 是正确的。例如:NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], @"someKey", nil];
  • 值得指出的是,现在有 NSNumbers 的文字语法。 @YES[NSNumber numberWithBool:YES] 相同
【解决方案2】:

Apple LLVM Compiler 4.0之后的新语法

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

语法将BOOL 转换为NSNumberNSDictionary 可以接受。

【讨论】:

    【解决方案3】:

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

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

    更多信息:

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

    【讨论】:

    • 获得编译器错误:使用 NSDictionary 类型的表达式初始化 NSMutableDictionary * 的指针类型不兼容。如果将声明改为 NSDictionary,则会出现编译器错误:Expected method to write dictionary element not found on object of type NSDictionary *
    • 文字只会创建NSDictionary,而不是NSMutableDictionary。所以将@YES 分配给foo[@"bar"] 是不可能的,因为@{ @"key": @NO } 是不可变的。
    【解决方案4】:

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

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

    【讨论】:

      【解决方案5】:

      试试这个:

      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'");
      }
      

      输出如下:

      对于 'Pratik',布尔值是 TRUE

      对于 'Sachin',布尔值是 FALSE

      【讨论】:

      • 你也可以[NSNumber numberWithBool:NO][NSNumber numberWithBool:YES]
      猜你喜欢
      • 2012-10-15
      • 2013-04-25
      • 2012-01-06
      • 1970-01-01
      • 2019-12-20
      • 2015-02-15
      • 1970-01-01
      • 2021-04-08
      • 1970-01-01
      相关资源
      最近更新 更多