1

I am new in Swift. My question is I am not sure how to unwrapping the optional value. When I print the object.objectForKey("profile_picture"), I can see Optional(<PFFile: 0x7fb3fd8344d0>).

    let userQuery = PFUser.query()
    //first_name is unique in Parse. So, I expect there is only 1 object I can find.
    userQuery?.whereKey("first_name", equalTo: currentUser)
    userQuery?.findObjectsInBackgroundWithBlock({ (objects: [PFObject]?, error: NSError?) -> Void in
        if error != nil {
        }
        for object in objects! {
            if object.objectForKey("profile_picture") != nil {
                print(object.objectForKey("profile_picture"))
                self.userProfilePicture.image = UIImage(data: object.objectForKey("profile_pricture")! as! NSData)
            }
        }
    })
Pranav Wadhwa
  • 7,666
  • 6
  • 39
  • 61
Pak Ho Cheung
  • 1,382
  • 6
  • 22
  • 52

1 Answers1

1

You'd use if let to perform "optional binding", only performing the block if the result in question is not nil (and binding the variable profilePicture to the unwrapped value in the process).

It would be something like:

userQuery?.findObjectsInBackgroundWithBlock { objects, error in
    guard error == nil && objects != nil else {
        print(error)
        return
    }
    for object in objects! {
        if let profilePicture = object.objectForKey("profile_picture") as? PFFile {
            print(profilePicture)
            do {
                let data = try profilePicture.getData()
                self.userProfilePicture.image = UIImage(data: data)
            } catch let imageDataError {
                print(imageDataError)
            }
        }
    }
}

Or, if you want to get data asynchronously, perhaps:

userQuery?.findObjectsInBackgroundWithBlock { objects, error in
    guard error == nil && objects != nil else {
        print(error)
        return
    }
    for object in objects! {
        if let profilePicture = object.objectForKey("profile_picture") as? PFFile {
            profilePicture.getDataInBackgroundWithBlock { data, error in
                guard data != nil && error == nil else {
                    print(error)
                    return
                }
                self.userProfilePicture.image = UIImage(data: data!)
            }
        }
    }
}

It would be something along those lines, using if let to unwrap that optional. And you then have to get the NSData associated with that the PFFile object (from the getData method or getDataInBackgroundWithBlock, presumably).

See Optional Binding discussion in The Swift Programming Language.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • It works. Would you mind to explain more what 'if let" is? – Pak Ho Cheung Jan 25 '16 at 00:19
  • `if let` is "optional binding", i.e. only performing the block if the result in question is not `nil` (and binding the variable `profilePicture` to the unwrapped value in the process). Obviously, if the object was `nil`, it would just jump past the `if let` block. See [Optional Binding](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-ID333) in _The Swift Programming Language._ – Rob Jan 25 '16 at 00:24
  • OK. Got it. Thank you:) – Pak Ho Cheung Jan 25 '16 at 01:32