Parse / Swift致命错误:使用未实现的初始化程序'init()

时间:2015-07-10 15:30:38

标签: ios swift parse-platform

如果有任何人使用Swift使用Parse的经验,特别是子类化PFObject .....我无法弄清楚为什么下面的saveinbackground调用会抛出上述错误?

谢谢!

func saveNewPerson(name: String) {

        var myPeeps = [Person]()

        if let currentUser = PFUser.currentUser() {

            if currentUser.valueForKey("myPeeps")?.count < 1 {
                myPeeps = []
            } else {
                myPeeps = currentUser.valueForKey("myPeeps") as! [Person]
            }

            let newPerson = Person(name: name, stores: [:])

        myPeeps.append(newPerson)

        currentUser.setObject(myPeeps, forKey: "myPeeps")

        println(currentUser.valueForKey("myPeeps")?.count)


                //WHY DOES THIS SAVE THROW ERROR FOR NOT INITIALZING?
        currentUser.saveInBackgroundWithBlock{ succeeded, error in
            if succeeded {
                //3
                println("Saved")
            } else {
                //4
                if let errorMessage = error?.userInfo?["error"] as? String {
                    self.showErrorView(error!)
                    }
                }
            }
        }
    }

这是我的Person类:

class Person: PFObject, PFSubclassing {

    override class func initialize() {
        struct Static {
            static var onceToken : dispatch_once_t = 0;
        }
        dispatch_once(&Static.onceToken) {
            self.registerSubclass()
        }
    }

    static func parseClassName() -> String {
        return "Person"
    }

    var name: String = ""
    var stores: [String : Store] = [:]

    init(name: String, stores: [String : Store]) {
    self.name = name
    self.stores = stores

    super.init()
}

}

我的商店类:

class Store: PFObject, PFSubclassing {


    override class func initialize() {
        struct Static {
            static var onceToken : dispatch_once_t = 0;
        }
        dispatch_once(&Static.onceToken) {
            self.registerSubclass()
        }
    }

    static func parseClassName() -> String {
        return "Store"
    }


    var name: String = ""
    var clothingSizes: [String: String] = [:]


    init(name: String, clothingSizes: [String: String]){
        self.name = name
        self.clothingSizes = clothingSizes
        super.init()
    }
}

1 个答案:

答案 0 :(得分:5)

对于两个Parse子类,您需要提高便利性。基本上,正在发生的是没有init()的实现,你可以通过调用

来实现
override init() {

    super.init()    

}

另一个选择是让你的init成为一个方便的init,并调用self.init()

convenience init(name: String, stores: [String : Store]) {

    self.init()
    self.name = name
    self.stores = stores

}