Search code examples
swiftrealmrealm-list

Init list values using Realm swift


I'm trying to initialize a couple of objects, which include List Objects but I'm not sure what the exact semantics are. Is it even possible to initialize List objects from within a Realm object or do I have to append them in some other way?

class ResourcesData: Object {
    @objc dynamic var resourceID = UUID().uuidString
    @objc dynamic var name = "";

    let valueSemantics = List<ValueSemantics>();

    convenience init(resourceName: String, semantics: List<ValueSemantics>) {
        self.init()
        self.name = resourceName
        self.valueSemantics = semantics
    }

    override static func primaryKey() -> String? {
        return "resourceID"
    }
}

My List model with initializer

class ValueSemantics: Object {
    @objc dynamic var name = "";
    @objc dynamic var value = 0;

    convenience init(value: Int, valueName: String) {
        self.init()
        self.name = valueName
        self.value = value
    }
}

Here is how I'm trying to initialize my object but it throws the following error '<' is not a postfix unary operator

let data: [ResourcesData] = [
  ResourcesData(resourceName: "Iron ore",
                semantics: List<
                  ValueSemantics(value: 0, valueName: "None"),
                  ValueSemantics(value: 1, valueName: "Average")
                  >
  ),
  ResourcesData(resourceName: "Gold ore",
                semantics: List<
                  ValueSemantics(value: 0, valueName: "None"),
                  ValueSemantics(value: 1, valueName: "Average")
                  >
  )   
];

do {
    try realm.write {
        realm.add(data);
    }
}catch {
    print("Error starting Realm! \(error)");
}

I also tried initializing it in the following way

ResourcesData(resourceName: "Gold ore",
                    semantics: List(
                      ValueSemantics(value: 0, valueName: "None"),
                      ValueSemantics(value: 1, valueName: "Average")
                      ))

As well as

ResourcesData(resourceName: "Gold ore",
                    semantics: [
                                ValueSemantics(value: 0, valueName: "None"),
                                ValueSemantics(value: 1, valueName: "Average")])

Solution

  • List only has an empty initialiser, so the only way to add values to a List is using append. You're better off modifying the initialiser of ResourcesData to accept an Array and call append(contentsOf:) to add the elements of the array to the list.

    class ResourcesData: Object {
        @objc dynamic var resourceID = UUID().uuidString
        @objc dynamic var name = ""
    
        let valueSemantics = List<ValueSemantics>()
    
        convenience init(resourceName: String, semantics: [ValueSemantics]) {
            self.init()
            self.name = resourceName
            self.valueSemantics.append(objectsIn: semantics)
        }
    
        override static func primaryKey() -> String? {
            return "resourceID"
        }
    }
    

    And then you can initialise it like this:

    let data: [ResourcesData] = [
      ResourcesData(resourceName: "Iron ore",
                    semantics: [ValueSemantics(value: 0, valueName: "None"),
                      ValueSemantics(value: 1, valueName: "Average")]
      ),
      ResourcesData(resourceName: "Gold ore",
                    semantics: [ValueSemantics(value: 0, valueName: "None"),
                      ValueSemantics(value: 1, valueName: "Average")]
      )   
    ]
    

    Unrelated to your issue, but you shouldn't put semicolons at the end of lines in Swift, this is not Objective-C.