我正在尝试计算我的列表中有多少张卡片是相等的,并用计数更新新的数量属性
例如:
newObject!["list"] = [CardObject1, CardObject2, CardObject2, CardObject2, CardObject3, CardObject3]
分配到临时列表
var tempList = List()
CardObject1(将数量属性更新为 1)
CardObject2(将数量属性更新为 3)
CardObject3(将数量属性更新为 2)tempList = [CardObject1,CardObject2,CardObject3]
分配回 newObject!["list"] 更新/迁移的列表
新对象![“列表”] = 新列表
在newList.index(of: card)处崩溃
* 由于未捕获的异常“RLMException”而终止应用程序,原因:“对象类型“CardDTO”与 RLMArray 类型“DynamicObject”不匹配。*首先抛出调用栈:
信息:
甲板DTO.swift
class DeckDTO: Object {
dynamic var id = NSUUID().uuidString
dynamic var name = ""
var list = List<CardDTO>()
override class func primaryKey() -> String {
return "id"
}
}
CardDTO.swift
class CardDTO: Object, Mappable {
// Other properties
dynamic var id = NSUUID().uuidString
dynamic var quantity: Int = 1
// Other properties
required convenience public init?(map: Map) {
self.init()
mapping(map: map)
}
func mapping(map: Map) {
//Map all my properties
}
override class func primaryKey() -> String {
return "id"
}
}
我正在尝试什么
if oldSchemaVersion < 2 {
migration.enumerateObjects(ofType: CardDTO.className()) { oldObject, newObject in
newObject!["quantity"] = 1
}
migration.enumerateObjects(ofType: DeckDTO.className()) { oldObject, newObject in
var newList = List<DynamicObject>()
let oldList = newObject!["list"] as! List<DynamicObject>
for card in oldList {
if let i = newList.index(of: card), i >= 0 {
newList[i] = (newList[i] as! CardDTO).quantity += 1 //some how do quantity++
print("increment quantity")
} else {
newList.append(card)
}
}
newObject!["list"] = newList
}
}