自定义初始化程序与对象

时间:2016-03-16 13:15:37

标签: ios swift realm

Class UserLaunch: Object{

var launch: Class1
var payload: Class2

init(launch: Class1, payload: Class2){
    self.launch = launch
    self.payload = payload
}

为Realm对象创建自定义初始值设定项的最佳方法是什么? (这个没有用,因为Realm想要默认的init()方法)

谢谢!

1 个答案:

答案 0 :(得分:2)

如果您可以为属性提供一些默认值,这应该有效(至少对我有用):

class UserLaunch: Object{

    dynamic var launch: Class1 = Class1() //or some other default value
    dynamic var payload: Class2 = Class2() //or some other default value

    convenience init(launch: Class1, payload: Class2){
        self.init()
        self.launch = launch
        self.payload = payload
    }
}