不能使用类型为'(String?!)的参数列表调用'append'

时间:2023-01-23 22:05:53

I'm trying to add usernames from Parse in an array to display them in a UITableView, but get an error when I'm appending the usernames to my array.

我试图在数组中添加来自Parse的用户名,以便在UITableView中显示它们,但是当我将用户名附加到数组时,会得到一个错误。

The error I get is: Cannot invoke 'append' with an argument list of type '(String?!)'

我得到的错误是:不能使用类型“(String?!)”的参数列表来调用“append”。

What am I doing wrong?

我做错了什么?

var usernames = [""]

func updateUsers() {      
    var query = PFUser.query()
    query!.whereKey("username", notEqualTo: PFUser.currentUser()!.username!)
    var fetchedUsers = query!.findObjects()

    for fetchedUser in fetchedUsers! {
        self.usernames.append(fetchedUser.username)
    }
}

2 个解决方案

#1


2  

I solved my problem. I declare the array as an empty array and for unwrap the optional with the following code:

我解决了我的问题。我将数组声明为空数组,并将可选项展开,代码如下:

var usernames = [String]()

self.usernames.removeAll(keepCapacity: true)

    for fetchedUser in fetchedUsers! {
        if let username = fetchedUser.username as String! {
            self.usernames.append(username)
        }
    }

#2


0  

PFUser.username is an optional, and you can't append an optional into a String array in Swift. This is because the optional can be nil, and a String array in Swift only accepts strings.

PFUser。用户名是可选的,不能在Swift中将可选的添加到字符串数组中。这是因为可选项可以是nil, Swift中的字符串数组只接受字符串。

You need to either force unwrap the optional, or use if-let syntax to add it if it exists.

您需要强制展开可选的,或者使用if-let语法添加可选的。

Force Unwrap

力打开

self.usernames.append(fetchedUser.username! as String)

Or if-let

或者如果让

if let name = fetchedUser.username as? String {
  self.usernames.append(name)
}

Plus as NRKirby mentions in the comments on your question, you might want to look at initialising the usernames array differently. At the moment the first element is an empty string.

此外,正如NRKirby在关于您的问题的评论中提到的,您可能想看看如何以不同的方式初始化usernames数组。此时,第一个元素是空字符串。

#1


2  

I solved my problem. I declare the array as an empty array and for unwrap the optional with the following code:

我解决了我的问题。我将数组声明为空数组,并将可选项展开,代码如下:

var usernames = [String]()

self.usernames.removeAll(keepCapacity: true)

    for fetchedUser in fetchedUsers! {
        if let username = fetchedUser.username as String! {
            self.usernames.append(username)
        }
    }

#2


0  

PFUser.username is an optional, and you can't append an optional into a String array in Swift. This is because the optional can be nil, and a String array in Swift only accepts strings.

PFUser。用户名是可选的,不能在Swift中将可选的添加到字符串数组中。这是因为可选项可以是nil, Swift中的字符串数组只接受字符串。

You need to either force unwrap the optional, or use if-let syntax to add it if it exists.

您需要强制展开可选的,或者使用if-let语法添加可选的。

Force Unwrap

力打开

self.usernames.append(fetchedUser.username! as String)

Or if-let

或者如果让

if let name = fetchedUser.username as? String {
  self.usernames.append(name)
}

Plus as NRKirby mentions in the comments on your question, you might want to look at initialising the usernames array differently. At the moment the first element is an empty string.

此外,正如NRKirby在关于您的问题的评论中提到的,您可能想看看如何以不同的方式初始化usernames数组。此时,第一个元素是空字符串。