iPhone - Core Data Bool Values?
I hav开发者_如何学Goe a BOOL in my core data. I want the value to be null unless i set it to either YES, or NO. Is it possible to do this? in my code how can i check to see if the value has not been set yet or not?
In my nsmanagedObject i use NSNumber for my BOOL, in my core data mdoel i have BOOL.
CoreData allows you to set default values. A Bool can contain "YES", "NO" , "None". Setting the default value to "None" makes the returned value "null" which is exactly what i needed.
If you set Boolean as the attribute type in your model, that means it will be implemented using NSNumber
.
I'm not sure why you'd want to return no value instead of NO
, I'd actually recommend you don't. You'll have a much easier time with data integrity if you just set a false default value.
However, if you absolutely insist, you can just check if the attribute is nil
or not to see if it was assigned a value yet.
The correct answer is yes, and there is real meaning to use 'yes' 'no' and a different 'none', which means the data is intact. In Core Data there is good reason sometimes to check against 'none', especially if you'd like to debug a database. In Core Data you can use:
[myManagedObject setValue: nil forKey:@"attributeOfMyManagedObject"];
and if check your SQL database behind your Core Data you will see 'NULL' if you use default settings.
精彩评论