代码之家  ›  专栏  ›  技术社区  ›  Greg Sexton

获得可空布尔值的Objective-C方法是什么?

  •  16
  • Greg Sexton  · 技术社区  · 14 年前

    我应该如何得到一个bool值,我可以在Objective-C中指定true、false和nil?什么是Objective-C方法?很像C可为空。

    我希望能够使用nil值来表示未定义的。

    2 回复  |  直到 13 年前
        1
  •  32
  •   dreamlax    14 年前

    NSNumber 实例可能会有所帮助。例如:

    NSNumber *yesNoOrNil;
    
    yesNoOrNil = [NSNumber numberWithBool:YES]; // set to YES
    yesNoOrNil = [NSNumber numberWithBool:NO];  // set to NO
    yesNoOrNil = nil; // not set to YES or NO
    

    为了确定其价值:

    if (yesNoOrNil == nil)
    {
        NSLog (@"Value is missing!");
    }
    else if ([yesNoOrNil boolValue] == YES)
    {
        NSLog (@"Value is YES");
    }
    else if ([yesNoOrNil boolValue] == NO)
    {
        NSLog (@"Value is NO");
    }
    

    -[NSNumber boolValue]

        2
  •  1
  •   Vladimir    14 年前

    我认为你需要使用一些类,例如wrap bool to NSNumber 对象。