2

Here I am with another question.

As I was coding through my app, I'm using Parse Framework, and I'm using a query to obtain the username off the database:

var showUsername:PFQuery = PFUser.query()!
    //showUsername.whereKey("objectId", equalTo: post.objectForKey("user")!.objectId)

    showUsername.whereKey("objectId", equalTo: post.objectForKey("user")!)

    showUsername.findObjectsInBackgroundWithBlock{
        (objects: [AnyObject]?, error: NSError?) -> Void in

         if error == nil{
            let user:PFUser = (objects as NSArray!).lastObject as! PFUser
            cell.usernameLabel.text = user.username
        }

    }

I am having trouble with this two lines of code:

//showUsername.whereKey("objectId", equalTo: post.objectForKey("user")!.objectId)

this one I have it commented out because it says that with the .objectID is wrong, so if I take that out, it says it's fine. So I was wondering if the function of it would be the same???

And also, this line :

let user:PFUser = (objects as NSArray!).lastObject as! PFUser

It shows on Xcode that is correct, but when I run the app, it freezes and crashed because of that line of code due to the error that says that "[AnyObject]? is not convertible to 'NSArray'

any ways to work around this?

I already tried what this other links say:

"'[AnyObject]?' is not convertible to 'NSArray'"

"How to set the current user in cell? Swift, Parse"

And I get no errors in my code, and it runs, but the username is not changed to the actual username, it still says "Label"

unless it has to do with the query missing the .objectID?

Community
  • 1
  • 1
Bruno Recillas
  • 951
  • 1
  • 9
  • 18
  • Try `(objects as NSArray?)?.lastObject as! PFUser`. Anyway, Swift arrays also have a last property. So you could use `let user:PFUser? = objects.last as? PFUser` – nielsbot Aug 06 '15 at 18:57
  • When I try that, it says "Cast from '[AnyObject]?' to unrelated type 'NSArray' always fails" – Bruno Recillas Aug 06 '15 at 19:01

2 Answers2

2

The first part of your question (the line you commented out):

In showUsername.whereKey("objectId", equalTo: post.objectForKey("user")!.objectId)

unwrap the objectId by adding an ! as follows

showUsername.whereKey("objectId", equalTo: post.objectForKey("user")!.objectId!)

The second part of your question:

Replace

let user:PFUser = (objects as NSArray!).lastObject as! PFUser

by

let user = (objects as! [PFUser]).last
Peter Fennema
  • 1,650
  • 12
  • 14
  • This did it! After trying this it showed the username on the post! Thank you (: – Bruno Recillas Aug 06 '15 at 19:22
  • 1
    Probably more Swift-y to use this: `let user = (objects as? [PFUser])?.last` That way the app won't crash when `objects == nil`. – nielsbot Aug 08 '15 at 17:54
  • @nielsbot I agree with you from the perspective of safety. When studying the docs and examples from Parse.com I concluded that in case error == nil objects can never be nil, so I considered it safe to unwrap it immediately. But rethinking I do prefer your suggestion. – Peter Fennema Aug 14 '15 at 08:51
1

Try this:

(objects as? NSArray)?.lastObject as! PFUser

Looks like objects is an optional array of AnyObject. You can cast it to an optional NSArray.

Anyway, Swift arrays also have a last property. You could use

`let user:PFUser? = objects.last as? PFUser`

instead. Your inner condition code can be written as

let user = objects?.last as? PFUser
cell.usernameLabel.text = user?.username ?? ""

Some explanation:

User is an optional PFUser, a PFUser reference that is a PFUser instance or nothing. The type is Optional. (Which can also be written PFUser?).

objects?.last evaluates to objects.last when objectsnil and nil otherwise.

Now user refers to the last PFUser instance in objects or nothing (nil).

user?.username evaluates to user.username when usernil and nil otherwise.

Finally the ?? operator returns the left-hand expression if it is non-nil, and the right hand side if it is. I.e. if the username is nil, use the empty string instead ("")

nielsbot
  • 15,922
  • 4
  • 48
  • 73