【发布时间】:2015-10-11 20:14:45
【问题描述】:
请告诉我,如何按索引重新排列 Realm 对象列表中的项目? IE。我看起来像
let movingElement = array[oldIndex]
array.removeAtIndex(oldIndex)
array.insert(movingElement, atIndex: newIndex)
如果是随便的 Swift 数组的话。
但对于 Realm 中的列表,我不能做同样的事情:
let realm = try! Realm()
var all = try! Realm().objects(element)
realm.write {
all.removeAtIndex() // all of type
另一种选择是
let realm = try! Realm()
let element = try! Realm().objects(Element)[oldIndex]
realm.write{
realm.delete(element)
realm.add(...) // How to set index to place new object at?
}
但是如何在合适的位置插入元素呢?可能有一种适当的方法如何通过索引移动相同类型(类)领域的元素? 提前致谢!
【问题讨论】: