使用Realm Swift初始化列表值

时间:2018-11-02 15:14:49

标签: swift realm realm-list

我正在尝试初始化几个对象,其中包括列表对象,但是我不确定确切的语义是什么。甚至可以从Realm对象中初始化List对象,还是必须以其他方式附加它们?

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"
    }
}

我的带有初始化器的列表模型

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
    }
}

这是我尝试初始化对象的方式,但是它引发以下错误'<' 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)");
}

我还尝试通过以下方式对其进行初始化

ResourcesData(resourceName: "Gold ore",
                    semantics: List(
                      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")])

1 个答案:

答案 0 :(得分:2)

List仅具有一个空的初始化程序,因此向List添加值的唯一方法是使用append。最好将ResourcesData的初始化程序修改为接受Array,然后调用append(contentsOf:)将数组的元素添加到列表中。

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"
    }
}

然后您可以像这样初始化它:

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")]
  )   
]

与您的问题无关,但您不应在Swift中将分号放在行尾,这不是Objective-C。