Swift 为什么addNotificationBlock在空列表上崩溃?
我得到了一个在空列表上崩溃的小代码,但是当列表实际上是从对象子类Swift 为什么addNotificationBlock在空列表上崩溃?,swift,realm,Swift,Realm,我得到了一个在空列表上崩溃的小代码,但是当列表实际上是从对象子类PopContact 知道为什么会崩溃吗 class Item: Object { } class Container: Object { let items = List<Item>() } func testRealmListNotification(mContainer: Container?) { let list = mContainer?.items ?? List<Item&g
PopContact
知道为什么会崩溃吗
class Item: Object {
}
class Container: Object {
let items = List<Item>()
}
func testRealmListNotification(mContainer: Container?) {
let list = mContainer?.items ?? List<Item>()
let token = list.addNotificationBlock { (updatedList: List<Item>) -> () in
log(updatedList)
}
//dispose of token later
}
类项:对象{
}
类容器:对象{
让项目=列表()
}
func testRealmListNotification(mContainer:容器?){
let list=mContainer?.items±list()
让token=list.addNotificationBlock{(updatedList:list)->()位于
日志(更新列表)
}
//稍后处置代币
}
在提交包含对象的领域的写入事务后发送通知,因此尚未添加到领域的新创建对象将永远不会有任何通知。与其静默地什么都不做,addNotificationBlock()
抛出一个异常,提醒您它无法工作。我明白了,但为什么不将函数标记为抛出呢?目前,其公共函数addNotificationBlock(block:(RealmSwift.List)->()->RealmSwift.NotificationTokenSwift异常用于运行时错误,而不是前提条件冲突。