Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何解决:构造函数参数不可变_Ios_Swift - Fatal编程技术网

Ios 如何解决:构造函数参数不可变

Ios 如何解决:构造函数参数不可变,ios,swift,Ios,Swift,要重构函数,我尝试将其全局可用: func loadUsers(_ users: [User] , _ tableView: UITableView) { let usersRef = firebase.child("Users") usersRef.observe(.value, with: { snapshot in if snapshot.exists() { users.removeAll()

要重构函数,我尝试将其全局可用:

func loadUsers(_ users: [User] , _ tableView: UITableView) {

    let usersRef = firebase.child("Users")
    usersRef.observe(.value, with: { snapshot in
        if snapshot.exists() {

                users.removeAll()
                ...
    }
我收到:

无法对不可变值使用mutating member:“users”是“let”常量

当我设定:

func loadUsers(_ users: [User] , _ tableView: UITableView) {

    let usersRef = firebase.child("Users")
    usersRef.observe(.value, with: { snapshot in
        if snapshot.exists() {

            if var users = users as? [User] {
                users.removeAll()
                ...
}
它构建并运行,但我不断收到警告:

从“[User]”到“[User]”的条件转换始终成功

及 用于检查选项的“[User]”类型的非可选表达式

解决我的问题的最佳方法是什么?非常感谢你的帮助


PS,我需要删除
用户的原始值。

第一组代码中的错误是因为参数是不可变的

第二组代码中的错误是因为您试图将
if let
与非可选常量一起使用,并且您试图转换为相同的类型,从而使转换毫无意义

由于您希望使用此函数修改原始数组,因此需要将其设置为
inout
参数:

func loadUsers(_ users: inout [User] , _ tableView: UITableView) {
    let usersRef = firebase.child("Users")
    usersRef.observe(.value, with: { snapshot in
        if snapshot.exists() {
            users.removeAll()
            ...
}

您现在还需要在传递给
users
参数的
var
之前添加
&

我没有注意到闭包的使用。你引用的链接是你需要做的更好的解决方案。