Jacob Tuazon Jacob Tuazon - 6 months ago 10
Swift Question

fatal error: unexpectedly found nil while unwrapping an Optional value swift

Ok so I am trying to run this code but I keep on getting this error: fatal error: unexpectedly found nil while unwrapping an Optional value. I don't understand what it means or why im getting it. Can someone please help me. Thanks

query.whereKey("accepted", equalTo: PFUser.currentUser().username)
query.whereKey("username", containedIn: PFUser.currentUser()["accepted"] as [AnyObject])

Answer

All you have to do is this:

if let currentUser = PFUser.currentUser() {
    query.whereKey("accepted", equalTo: currentUser.username)
    if let someArrayObject = currentUser["accepted"] as? [AnyObject] {
        query.whereKey("username", containedIn: someArrayObject)
    }
} else {
    // currentUser does not exist, do error handling
}