1

I want the user to upload an image, and other users leave replies to that image. Everything works fine so far except if the row for that specific image object is empty, the app crashes.

fatal error: unexpectedly found nil while unwrapping an Optional value

Here is my code :

override func viewDidLoad() {
    super.viewDidLoad()

    var error = ""

    var query = PFQuery(className:"Posts")
    query.getObjectInBackgroundWithId(cellID) {
        (objects: PFObject!, error: NSError!) -> Void in
        if error == nil {      
            var array = objects.objectForKey("replies") as [String] // <- when error occurs the compiler point here.
            for object in  array {
                self.repliesArray.append(object as String)
            }
        } else { 
            self.displayError("Error", error: "Error retreiving")
        }
        self.tableView.reloadData()
    }
}

2 Answers 2

0

Does this work? You can't append null objects to an array so this appends an empty string instead of null. Also you need the explanation mark so that it can be nil.

var array = objects.objectForKey("replies") as [String!]
for object in array {
    if object != nil {
        self.repliesArray.append(object as String)
    }
    else {
        self.repliesArray.append("")
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Gives ann error, tried so many different If statements but kept giving me same error.
Error: Type 'NSString' does not conform to protocol 'NilLiteralConvertible'
0
query.getObjectInBackgroundWithId(cellID) {
    (objects: PFObject!, error: NSError!) -> Void in
    if error == nil {      
        var array = objects.objectForKey("replies") as [String] // <- when error occurs the compiler point here.

The key here is that you're accepting an implicitly unwrapped optional (PFObject!) which is implying a promise that it will never be nil, but is nil.

Ideally (*), the type should be (objects: PFObject?, error: NSError?) -> Void to make the optionals explicit and force you to do the nil checking that is required. I believe you can do this even if the caller claims to send you (PFObject!, NSError!) -> Void, since I think Swift will make the implicit->explicit conversion for you.

If that's still impossible, then you will have to manually verify that objects is not nil before using it. (This is unlike @Dehli's solution, which checks that the things contained in objects are non-nil. That's not the problem; the problem is that objects itself is nil.)

(*) I say "ideally" here in terms of the existing interface. Passing a tuple of optional value/error is a lousy pattern. The better solution is to use something like a Result object.

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.