Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.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 在使用Parse之前,必须向registerSubclass注册该类_Swift_Parse Platform_Key Value Observing_Pfobject - Fatal编程技术网

Swift 在使用Parse之前,必须向registerSubclass注册该类

Swift 在使用Parse之前,必须向registerSubclass注册该类,swift,parse-platform,key-value-observing,pfobject,Swift,Parse Platform,Key Value Observing,Pfobject,我对子类PFObject中的属性使用KVO,该属性在初始化期间已注册 如果我使用1个对象,一切都很好。在第二个对象上,我得到错误在使用Parse之前,类KVO\u vs\u PFObject.MyModel必须注册到registerSubclass。我需要多个对象来观察属性 我试图在swift上使用propertyobserver(didSet),但编译器不允许,因为我使用的是托管属性 有人知道这个代码是怎么回事吗 下面是我的代码: import UIKit import Parse clas

我对子类
PFObject
中的属性使用KVO,该属性在初始化期间已注册

如果我使用1个对象,一切都很好。在第二个对象上,我得到错误
在使用Parse之前,类KVO\u vs\u PFObject.MyModel必须注册到registerSubclass。
我需要多个对象来观察属性

我试图在swift上使用property
observer(didSet)
,但编译器不允许,因为我使用的是托管属性

有人知道这个代码是怎么回事吗

下面是我的代码:

import UIKit
import Parse

class MyModel : PFObject, PFSubclassing {
    static func parseClassName() -> String {
        return "MyModel"
    }
    override class func initialize() {
        var onceToken : dispatch_once_t = 0;
        dispatch_once(&onceToken) {
            self.registerSubclass()
        }
    }
    @NSManaged var property1 : String?
}

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        var myObject = MyModel()
        myObject.addObserver(self, forKeyPath: "property1", options: .New, context: nil)
        myObject.property1 = "Hello"
        myObject.removeObserver(self, forKeyPath: "property1")

        // If I comment these 4 lines. myObject is happy observing the property.
        var anotherObject = MyModel()
        anotherObject.addObserver(self, forKeyPath: "property1", options: .New, context: nil)
        anotherObject.property1 = "World"
        anotherObject.removeObserver(self, forKeyPath: "property1")
    }
    override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
        var n : AnyObject? = change["new"]
        switch keyPath {
        case "property1" :
            println("observed MyModel.property1 with value \(n)")
        default :
            break
        }
    }
}
导入UIKit
导入解析
类MyModel:PFObject,PFSubclassing{
静态func parseClassName()->字符串{
返回“MyModel”
}
重写类func initialize(){
var onceToken:dispatch\u once\u t=0;
一次发送(一次发送){
self.registerSubclass()
}
}
@NSVAR托管属性1:字符串?
}
类ViewController:UIViewController{
重写func viewDidLoad(){
super.viewDidLoad()
var myObject=MyModel()
myObject.addObserver(self,forKeyPath:“property1”,选项:。新建,上下文:nil)
myObject.property1=“您好”
myObject.removeObserver(自我,分叉路径:“属性1”)
//如果我评论这四行,myObject很乐意观察这个属性。
var anotherObject=MyModel()
另一个Object.addObserver(self,forKeyPath:“property1”,选项:。新建,上下文:nil)
另一个object.property1=“世界”
另一个Object.removeObserver(self,forKeyPath:“property1”)
}
重写func observeValueForKeyPath(键路径:字符串,对象对象:AnyObject,更改:[NSObject:AnyObject],上下文:UnsafeMutablePointer){
变量n:AnyObject?=更改[“新建”]
开关键路径{
案例“财产1”:
println(“观察到的MyModel.property1,值为\(n)”)
违约:
打破
}
}
}

根据相关帖子,我最终使用了计算属性而不是存储属性

/@NSManaged var property1:String?
var属性1:字符串?{
得到{
将self[“property1”]返回为?字符串
}
设置{
self[“property1”]=newValue
println(“具有值\(newValue)”的MyModel.property1)
}

}
除了initialize()之外,我的AppDelegate中还有这个。这里有一些帖子说initialize()在被调用之前需要一些启动。下面值得一试

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {


        // Override point for customization after application launc

        //-----------------Parse customizations------------------------------


        // Enable storing and querying data from Local Datastore.
        // Remove this line if you don't want to use Local Datastore features or want to use cachePolicy.
        Parse.enableLocalDatastore()

        // ****************************************************************************
        // Uncomment this line if you want to enable Crash Reporting
        // ParseCrashReporting.enable()
        //
        // Uncomment and fill in with your Parse credentials:

        //This one is for the MyAppName Parse App

        MyModel.registerSubclass()

        // Rest of the stuff...

谢谢你的邀请reply@Jitendra . 我尝试了appDelegate,但没有成功。如果在第二个实例上与KVO一起使用,仍然会出现错误。第一个例子很好。这只是第二次爆炸。对不起,埃德加多,那样的话,我也不知道发生了什么。当我搜索initialize时,我记得读到在调用initialize之前必须引用一次类。根据您第一次观察到的情况,初始化()后可能会出现问题。我的初始化代码:重写类func initialize(){struct Static{Static var onceToken:dispatch_once\u t=0;}dispatch_once(&Static.onceToken){self.registerSubclass()}