1

As per the Realm Documentation, CGFloat is not one of the types supported in Realm. So I have a Person Storage class as follows:-

class Person: Object {
@objc dynamic var height: Float = 0.0
@objc dynamic var weight: CGFloat = 5.0

override var description: String {
    return " { Height = \(height), Weight = \(weight)}"
 }
}

I know till the object of Person Class is added to realm, It will behave as normal swift object and will not be managed by Realm. Once it is added to the realm, Realm will manage it. So it will compile successfully.

Now I am adding the object to the realm, and then printing it.

let person = Person()
try! realm.write {
    realm.add(person)
}
print(realm.objects(Person.self).first)

It is printing Optional( { Height = 0.0, Weight = 5.0})

As per my understanding, since type CGFloat is not supported by Realm, it should crash at runtime when I am adding object to Realm.

So what is going under the hoods here? Also which Type is being used to save CGFloat value?

Keshav Raj
  • 376
  • 1
  • 7
  • 1
    ```/// The native type used to store the CGFloat, which is Float on 32-bit architectures and Double on 64-bit architectures.``` – user28434'mstep May 31 '19 at 09:44
  • 2
    “CGFloat properties are discouraged, as the type is not platform independent.” , that’s from the doc. Discouraged does not mean it’s not supported. You can probably find out more details by doing some research – Joakim Danielson May 31 '19 at 09:47
  • Makes Sense. Thanks... – Keshav Raj May 31 '19 at 09:50
  • Why do you need to manage a CGFloat? You could also use getters and setters to handle the Float->CGFloat if you class must only expose a CGFloat. – Jay May 31 '19 at 20:00
  • @Jay Its not about having custom properties....Its about how Realm is working – Keshav Raj Jun 01 '19 at 12:22
  • There are a couple of things. First is the comment from @JoakimDanielson above. CGFloat properties are supported but discouraged. A CGFloat is platform specific so I was asking why do you need a CGFloat as there may be other options that are better supported. Second thing is if you want to know a property type, you can create the object in Realm and then open the file with Realm Studio and you can see the property type; in this case the GGFloat is stored as a double 'under the hood'. Realm is working correctly and as advertised; was there more to the question or has it been answered? – Jay Jun 01 '19 at 12:38

0 Answers0