简体   繁体   中英

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

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?

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

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. Ie if the username is nil , use the empty string instead ( "" )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM