今天我发现了Realm,我对它并不熟悉。请帮忙。
我有2个班级:
class EventDetail: Object {
dynamic var start_time : Int = 0
dynamic var end_time : Int = 0
convenience init(start_time: Int, end_time: Int ) {
self.init()
self.start_time = start_time
self.end_time = end_time
}
class Channel : Object {
dynamic var id : Int = 0
convenience init ( id : Int) {
self.id = id
}
let details = List<EventDetail>()
}
我像这样填充数据库:
let realm = try! Realm()
var newChannel = Channel (id : id)
let detailEvent1 = EventDetail(start_time:10, end_time: 20)
let detailEvent2 = EventDetail(start_time:20,end_time: 30)
newChannel.details.append(detailEvent1)
newChannel.details.append(detailEvent2)
try! realm.write {
realm.add(newChannel)
}
问题是:如何删除/更新特定索引的详细信息?
Ex:从频道获取detailEvent2,其中index = 100
答案 0 :(得分:0)
您需要对要更改或删除的对象类型执行提取。
基本操作是
realm.objects(Channel.self)
要找出要更改或删除的对象,可以使用简单的高阶函数过滤结果,例如谓词:
let lateStarters = realm.objects(Channel.self).filter(NSPredicate(format: "start_time > 10", argumentArray nil))
并通过枚举结果数组来更改其属性:
try? realm.write({
for channel in lateStarters {
channel.start_time = 100
}
})