简体   繁体   中英

iPhone - Core Data Bool Values?

I have 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM