简体   繁体   English

“ [AnyObject]?无法转换为'NSArray'

[英]"[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: 在通过应用进行编码时,我正在使用Parse Framework,并且正在使用查询从数据库中获取用户名:

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. 我把它注释掉了,因为它说.objectID是错误的,所以如果我删除它,那说很好。 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' 它在Xcode上显示是正确的,但是当我运行该应用程序时,它由于该行代码而死机并崩溃,原因是该错误表明“ [AnyObject]?无法转换为'NSArray'

any ways to work around this? 有什么办法可以解决此问题?

I already tried what this other links say: 我已经尝试了其他链接的内容:

" '[AnyObject]?' '[AnyObject]? is not convertible to 'NSArray' " 不能转换为“ NSArray

" How to set the current user in cell? Swift, Parse " 如何在单元格中设置当前用户?Swift,解析

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" 而且我的代码没有错误,并且可以运行,但是用户名未更改为实际的用户名,仍然显示“ Label”

unless it has to do with the query missing the .objectID? 除非与查询缺少.objectID有关?

The first part of your question (the line you commented out): 问题的第一部分(您注释掉的行):

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

unwrap the objectId by adding an ! 通过添加!来解开objectId。 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. 看起来对象是AnyObject的可选数组。 You can cast it to an optional NSArray. 您可以将其强制转换为可选的NSArray。

Anyway, Swift arrays also have a last property. 无论如何,Swift数组也具有last属性。 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. 用户是可选的PFUser,是PFUser实例的PFUser引用,或者不包含。 The type is Optional . 类型是Optional (Which can also be written PFUser? ). (哪个也可以写为PFUser? )。

objects?.last evaluates to objects.last when objectsnil and nil otherwise. objectnil时, objects.last求值为objects?.last ,否则为nil

Now user refers to the last PFUser instance in objects or nothing ( nil ). 现在, 用户引用对象中的最后一个PFUser实例,否则为零nil )。

user?.username evaluates to user.username when usernil and nil otherwise. usernil时, user?.username评估为user.username ,否则为nil

Finally the ?? 最后?? operator returns the left-hand expression if it is non-nil, and the right hand side if it is. 如果为非nil,则运算符返回左侧表达式,如果为非,则返回右侧。 Ie if the username is nil , use the empty string instead ( "" ) 也就是说,如果用户名是nil ,请使用空字符串代替( “”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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