如何将新的ArrayList(作为字段)添加到realm db

时间:2017-07-05 08:28:30

标签: ios swift

当我尝试将新字段(“iconImage”)添加到模型中以及之后,我会收到类似“因未捕获的异常而终止应用程序'RLMException'的错误,原因:'初始化对象的值无效'IconImage'类型:缺少关键'imgUrl '。我该如何解决?

的AppDelegate:

let config = Realm.Configuration(

   schemaVersion: 18,

     migrationBlock: { migration, oldSchemaVersion in

        if (oldSchemaVersion < 18) {
             migration.enumerate(Place.className()) { oldObject, newObject in
                  newObject!["iconImage"] = List<IconImage>()
                    }
                }

        })

我的对象:

class IconImage: Object {
    dynamic var imgUrl: String = ""
}

class Place: Object {

    dynamic var id = 0
    dynamic var categoryID = 0

    var iconImage = List<IconImage>()
}

添加Realm db:

func addPlace(id: Int, categoryID: Int, iconImage: List<IconImage>) {

    let place = Place()
    place.id = id
    place.categoryID = categoryID
    place.iconImage = iconImage

    try! self.db?.write {
        self.db?.add(place, update: true)
    }
}

设置列表(我的错误)

let iconList = List<IconImage>()
for (_,image) in images {
     let img = IconImage()
     img.imgUrl = image.stringValue
     iconList.append(img)
}

0 个答案:

没有答案