When I try to add a new field to record type, there is no possibility to add a Bool type. How can I do this anyway?
Referring to Creating a Database Schema by Saving Records Apple Documentation, the following table contains CloudKit possible field types:
unfortunately, there is no Bool type found. What I find -logically- is the most suitable to be a replacement of Bool is the Int(64)
, because:
Int(64) class -as mentioned in the table- is NSNumber which has a boolValue
which returns a Swift Bool:
The number object's value expressed as a Boolean value. A 0 value always means false, and any nonzero value is interpreted as true.
For more details, check the NSNumber documentation.
Which leads to the ease of treating them as Bool inside your application. For example:
let myFalseNumber:NSNumber = 0.0
let myTrueNumber:NSNumber = 0.01232
let anotherTrueNumber: NSNumber = -99
print(myFalseNumber.boolValue) // false
print(myTrueNumber.boolValue) // true
print(anotherTrueNumber.boolValue) // true
Although both Int(64)
and Double
are treated as NSNumber, the reason why choosing Int(64)
instead of Double
(CloudKit Type) is the generality of treating "1 if true and 0 if false" (without any floating points). For more information, you might want to check Boolean data type - Generalities (Wikipedia).
Hope it helped.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With