Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create field with Bool type for Record Type?

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?

enter image description here

like image 956
Bartłomiej Semańczyk Avatar asked Jan 19 '17 11:01

Bartłomiej Semańczyk


1 Answers

Referring to Creating a Database Schema by Saving Records Apple Documentation, the following table contains CloudKit possible field types:

enter image description here

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.

like image 181
Ahmad F Avatar answered Oct 15 '22 22:10

Ahmad F