Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何在领域模型上设置观察者?_Swift_Realm - Fatal编程技术网

Swift 如何在领域模型上设置观察者?

Swift 如何在领域模型上设置观察者?,swift,realm,Swift,Realm,出于调试目的,我想在模型上设置观察者/观察者,但到目前为止我还没有找到提示 请注意,我对iOS开发相当陌生(不到一个月),因此我可能遗漏了一些东西。如果您想观察整个对象类,您可以进行查询,应用过滤器,然后观察这些结果 如果您想观察单个对象的更改,可以检索该对象,然后通过查看您感兴趣的属性。下面是一个关于领域中KVO(Swift)的示例: 仅用于演示KVO i领域如何与持久对象一起工作 class MyRealmClass: Object { dynamic var id = NSUUID

出于调试目的,我想在模型上设置观察者/观察者,但到目前为止我还没有找到提示


请注意,我对iOS开发相当陌生(不到一个月),因此我可能遗漏了一些东西。

如果您想观察整个对象类,您可以进行查询,应用过滤器,然后观察这些
结果


如果您想观察单个对象的更改,可以检索该对象,然后通过查看您感兴趣的属性。

下面是一个关于领域中KVO(Swift)的示例: 仅用于演示KVO i领域如何与持久对象一起工作

class MyRealmClass: Object {
    dynamic var id = NSUUID().UUIDString
    dynamic var date: NSDate?
    override static func primaryKey() -> String? {
        return "id"
    } 
}

class ViewController: UIViewController {
    var myObject: MyRealmClass?

    private var myContext = 0

    override func viewDidLoad() {
        super.viewDidLoad()

        myObject = MyRealmClass()
        try! uiRealm.write({ () -> Void in
            myObject?.date = NSDate()
            uiRealm.add(myObject!, update: true)
            print("New MyClass object initialized with date property set to \(myObject!.date!)")
        })

        myObject = uiRealm.objects(MyRealmClass).last
        myObject!.addObserver(self, forKeyPath: "date", options: .New, context: &myContext)

        //Sleep before updating the objects 'date' property.
        NSThread.sleepForTimeInterval(5)

        //Update the property (this will trigger the observeValueForKeyPath(_:object:change:context:) method)
        try! uiRealm.write({ () -> Void in
            myObject!.date = NSDate()
        })
    }

    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        if context == &myContext {
            print("Date property has changed, new value is \(change![NSKeyValueChangeNewKey]!)")
        }
    }
 }
类MyRealmClass:Object{
动态变量id=nsuid().uuiString
动态var日期:NSDate?
重写静态func primaryKey()->字符串{
返回“id”
} 
}
类ViewController:UIViewController{
var myObject:MyRealmClass?
私有变量myContext=0
重写func viewDidLoad(){
super.viewDidLoad()
myObject=MyRealmClass()
试试!uiRealm.write({()->Void in)
myObject?.date=NSDate()
添加(myObject!,更新:true)
打印(“新MyClass对象已初始化,日期属性设置为\(myObject!.date!)”)
})
myObject=uiRealm.objects(MyRealmClass).last
myObject!.addObserver(self,forKeyPath:“日期”,选项:。新建,上下文:&myContext)
//在更新对象的“日期”属性之前睡眠。
NSThread.sleepForTimeInterval(5)
//更新属性(这将触发observeValueForKeyPath(u3;:对象:更改:上下文:)方法)
试试!uiRealm.write({()->Void in)
myObject!.date=NSDate()
})
}
重写func observeValueForKeyPath(键路径:String?,对象对象的类型:AnyObject?,更改:[String:AnyObject]?,上下文:UnsafeMutablePointer){
如果上下文==&myContext{
打印(“日期属性已更改,新值为\(更改![NSKeyValueChangeNewKey]!)”)
}
}
}

您是否尝试过使用Realm Browser