ray john
ray john

Reputation: 261

Check if PFObject is nil

I want to check if a PFObject is nil

I retrieve the object

var userLibrary: PFObject?

func getUserLibrary(user: PFUser) {
    self.libraryQuery = PFQuery(className: "Library")
    self.libraryQuery?.whereKey("owner", equalTo: user)

    self.libraryQuery?.findObjectsInBackgroundWithBlock({ (objects: [PFObject]?, error: NSError?) -> Void in
        if error == nil {
            if objects!.count > 0 {
                self.userLibrary = objects![0]
            } else {
                print(self.userLibrary)
            }
        }
    })
}

The last line with the print statement prints out nil.

However when I check :

if userLibrary != nil {
}

Xcode tells me

Binary operator '!=' cannot be applied to operands of type 'PFObject' and 'NilLiteralConvertible'

How do I fix this ?

Upvotes: 0

Views: 396

Answers (1)

Anthony Dito
Anthony Dito

Reputation: 3670

I'm not 100% sure that this will work, but did you try.

if let lib = userLibrary {
     //do whatever
}

Let me know.


Also, if you are just using the first object of your query. It would better to use getFirstObjectInBackground

Upvotes: 1

Related Questions