Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have implemented a Parse subclass called Filler.swiftthat contains five variables that are held in the Parse backend.

Whilst trying to save data to the Parse backend using this subclass I get the error: fatal error: unexpectedly found nil while unwrapping an Optional value.

This is the code that brings about the error (on the self.object.username = username line):

if let username = PFUser.currentUser()?.username {

        // set username equal to current user
        self.object.username = username

    }else{

        println("PFUser.currentUser()?.username contained a nil value.")

    }

I've figured out that it's something to do with how I'm handling optional variables in my subclass but the Parse documentation isn't clear on exactly how to do this. Here's my code for the subclass:

class Fillup : PFObject, PFSubclassing {

var amount : String? {

    get {

        return self["amount"] as? String

    }

    set{

        self["amount"] = newValue

    }

}

var cost : String? {

    get {

        return self["cost"] as? String

    }

    set{

        self["cost"] = newValue

    }

}


var date : NSDate {

    get {

        return self["date"] as! NSDate

    }

    set{

        self["date"] = newValue

    }

}

var username: String? {

    get{

        return self["username"] as? String

    }

    set {


        self["username"] = newValue

    }
}


var id : Int?{

    get {

        return self["id"] as? Int

    }

}

override class func initialize() {

    var onceToken : dispatch_once_t = 0;

    dispatch_once(&onceToken) {

        self.registerSubclass()

    }

}

class func parseClassName() -> String {

    return "Fillup"
}

}

Any help would be really appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
343 views
Welcome To Ask or Share your Answers For Others

1 Answer

Did you try adding a ? like so: self.object?.username = username


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...