iPhone: Save boolean into Core Data

后端 未结 4 1878
情话喂你
情话喂你 2021-01-30 00:47

I have set up one of my core data attributes as a Boolean. Now, I need to set it, but XCode keeps telling me that it may not respond to setUseGPS.

[ride setUseG         


        
4条回答
  •  -上瘾入骨i
    2021-01-30 01:25

    As an alternative approach to the accepted answer, you can simply change the typing from an NSNumber* to a BOOL in the managed object interface definition, such as:

    @property (nonatomic) BOOL useGPS;   // Notice that the 'retain' is also removed as we're now dealing with a scalar rather than an NSObject
    

    Various alternative approaches are discussed here, but Chris Hanson's response was most illuminating for me, especially:

    If you have a numeric attribute (including a boolean attribute) that's required, you can just type it as a scalar instead, and Core Data will do the right thing:

    @property (nonatomic) BOOL isDone;

    Even if the attribute is optional, that'll still work - it'll just conflate "not present" with "false."

    and for a more aligned Cocoa implementation:

    One other thing you might want to do is name the property "done" and just specify the getter as "isDone." That's the usual Cocoa naming convention:

    @property (nonatomic, getter=isDone) BOOL done;

    Then you can write "if (item.done) { ... }" or "item.done = NO;" and the compiler will still generate -isDone for accesses of the property.

    Thanks Chris, and hope that this helps someone.

提交回复
热议问题