1

If anyone has any experience working with Parse using Swift, specifically subclassing PFObject..... I cannot figure out why the saveinbackground call below is throwing the above error?

Thanks!

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!)
                    }
                }
            }
        }
    }

This is my Person class:

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()
}

}

My Store Class:

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()
    }
}
6
  • @luk2302 No, omitting the parentheses is perfectly fine. Commented Jul 10, 2015 at 15:36
  • @matt okay, wasn´t sure. Commented Jul 10, 2015 at 15:37
  • @PSU Exactly which line does the error happen on? Commented Jul 10, 2015 at 15:38
  • By the way, this code makes no sense: if let errorMessage = error?.userInfo?["error"] as? String {self.showErrorView(error!)} You are creating a variable errorMessage but not using it for anything. In Swift 2.0 you'll be slapped down for doing that. Commented Jul 10, 2015 at 15:40
  • @PSU What is the Store class? I would think that would have to be a PFObject subclass too, since it's a referenced property of Person. If it's not that seems like a likely caus Commented Jul 10, 2015 at 15:42

1 Answer 1

7

For both Parse subclasses, you need to make your inits convenience inits. Basically, what's going on is there is no implementation of init(), which you could do, by calling

override init() {

    super.init()    

}

Another option is to make your init a convenience init, and calling self.init()

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

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

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.