How can I add a boolean value to a NSDictionary?

后端 未结 5 1743
长情又很酷
长情又很酷 2021-01-30 05:46

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?

5条回答
  •  盖世英雄少女心
    2021-01-30 06:31

    You use NSNumber.

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

    From the NSNumber class reference:

    // 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
    

提交回复
热议问题