-1

I want to use realm to save data but as far as I understand realm does not support CGFloat and Color property, I searched for solution but it seems there is no answer.

Saving properties not supported by RealmSwift

In documentation said : “CGFloat properties are discouraged, as the type is not platform independent.”

In my app I use DragGesture and the value that returns for width and height are CGFloat this is my struct:

struct Card : Identifiable {
  
  var id = UUID()
  var name : String
  var color : Color
  var y : CGFloat = 0
  var x : CGFloat = 0
  
}

and DB so far :

class cardDB : Object {
  
  @objc dynamic var id = UUID()
  @objc dynamic var name = ""
  
}

Is there any way to save these two property to realm DB?

Raja Kishan
  • 16,767
  • 2
  • 26
  • 52
fdvfarzin
  • 1,107
  • 1
  • 4
  • 14
  • 2
    There are many other ways to represent these things. For the `CGFloat`, you can use a `Float` or `Double` instead. For the `Color`, you can store a hex string, 3 or 4 integers representing RGB(A), or a string representing the name of a color that your app would recognise. Choose the best representation that suits your specific situation, which you haven't described. – Sweeper Jun 28 '21 at 11:20
  • As you said I used Float instead of CGFloat and it works could you explain more about Color? – fdvfarzin Jun 28 '21 at 11:45
  • The structure in your question has a property `var color : Color` so he was stating we don't know what that `Color` type is. Perhaps a UIColor? – Jay Jun 28 '21 at 20:49

1 Answers1

1

Map CGFloat to Float.

I’m not sure what your Color type is. Assuming it’s like UIColor, and is in RGBA format, you should be able to create a struct containing Float values for red, green, blue, and alpha and save those.

Edit:

Your Card struct contains the following property:

var color : Color

That declares a variable color of type Color. Color Is not a system type. It might as well read

var color : CatFood

We have no idea what type Color type is. Based on the name, we can guess that it contains a description of a color, but that’s a guess.

You should command-click on the type Color in your project and select “jump to definition”. Then see what that type is. If it is an alias of UIColor or CGColor, great. If it is some custom type, add that definition to the bottom of your question.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • I changed the CGFloat to Float it works what do you mean Color type? I don't get it could you explain more about Color – fdvfarzin Jun 28 '21 at 11:48
  • @fdvfarzin The structure in your question as a property `var color : Color` so he was stating we don't know what that `Color` type is. Perhaps a UIColor? – Jay Jun 28 '21 at 18:06