这是我想要在Realm中交换子对象(origin和destination)的代码:
class TrainSearchOptions: Object {
dynamic var origin: Station?
dynamic var destination: Station?
dynamic var date: Date?
func swapPlaces() {
guard origin != nil && destination != nil else {
fatalError()
}
swap(&origin!, &destination!)
}
}
但这会崩溃。有什么想法吗?
答案 0 :(得分:0)
您需要在Realm写入事务中执行此操作。
do {
try realm.write {
swap(&origin!, &destination!)
}
} catch let error as NSError {
print(error)
}
}