swift - Swift 中的参数
问题描述
我已将我的 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”时,我想为用户名和帐户添加一个参数,有人可以帮我吗?非常感谢!
解决方案
我不知道什么类型的帐户。但语法应该是这样的:
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
}
推荐阅读
- python - Tesseract OCR 无法检测数字
- python - 如何访问 JSON 的特定部分以进行 API 调用 - Python
- php - WooCommerce 在欧盟保持相同的价格,无论国家税收如何
- mysql - 将mysql列拆分为多行
- curl - 如何销毁janus服务器中的会话ID?
- visual-studio-code - 在 VSCode 中,以编程方式打开 webview 的查找小部件
- javascript - 滑块照片不会随按钮更改
- angular - 角度虚拟滚动
- azure-cosmosdb-mongoapi - 使用 BulkExecutor .Net SDK 的 Azure Cosmos DB Mongo API 批量操作 - MongoBulkExecutor
- amazon-s3 - 使用 Multer-S3 使用 express 将高于几 MB 的文件直接上传到 S3