-1

我已将我的 Xcode 项目链接到 firebase 实时数据库。为了从数据库中读取,我使用了一个完成处理程序,一个变量可以从闭包中返回。但是,我想在调用完成处理程序时添加一个参数(变量帐户和用户名)。

以下是我的闭包和完成处理程序代码:

func getAccounts(completion: @escaping (_ noOfAccounts: Int) -> Void){
            ref.child("Accounts").observeSingleEvent(of: .value) {(snapshot:DataSnapshot) in
                let noOfAccounts = snapshot.value as? Int
                completion(noOfAccounts!)
            }
}


getAccounts() { (AccountNo) -> () in
            print(AccountNo)
}

当我调用函数“getAccounts”时,我想为用户名和帐户添加一个参数,有人可以帮我吗?非常感谢!

4

1 回答 1

1

我不知道什么类型的帐户。但语法应该是这样的:

func getAccounts(username: String, account: String, completion: @escaping (_ noOfAccounts: Int) -> Void){
        ref.child("Accounts").observeSingleEvent(of: .value) {(snapshot:DataSnapshot) in
            let noOfAccounts = snapshot.value as? Int
            completion(noOfAccounts!)
        }
}

调用该方法将如下所示:

getAccounts(username: ”someUsername”, account: ”someAccount”) { noOfAccounts in
    // Do something
}
于 2020-02-08T21:40:11.957 回答