gpt4 book ai didi

Swift 4 函数对于多个返回值返回 0

转载 作者:行者123 更新时间:2023-11-30 12:00:32 24 4
gpt4 key购买 nike

我一直在寻找但找不到解决方案。有人可以让我知道我做错了什么吗? 这是我的函数代码:在我的数据库中,我将 hour=5 作为 Int,将 Minute=45 作为 Int但是当我打印时,函数的值打印 0,0

var docRefF : DocumentReference!

func getTime()-> (Int, Int){
var FHour = Int()
var FMinute = Int()

docRefF = Firestore.firestore().document("sampleTime/worktime")
dataListener = docRefF.addSnapshotListener { (docSnapshot, error) in
guard let docSnapshot = docSnapshot, docSnapshot.exists else { return }

let data = docSnapshot.data()
let Hour:Int = data["Hour"]! as! Int
let Minute: Int = data["Minute"]! as! Int

FHour = Hour
FMinute = Minute
}

return (FHour, FMinute)
}
let time = getTime()
print("\(time.0),\(time.1)" )
//printed 0,0

最佳答案

显然是异步执行的问题。使用*函数与闭包*而不是具有返回值的函数。

请引用以下代码

var docRefF : DocumentReference!
func getTime(_ then:(_ first:Int, _ second:Int)->()){
var FHour = Int()
var FMinute = Int()


docRefF = Firestore.firestore().document("sampleTime/worktime")
dataListener = docRefF.addSnapshotListener { (docSnapshot, error) in
guard let docSnapshot = docSnapshot, docSnapshot.exists else { return }
let data = docSnapshot.data()
let Hour:Int = data["Hour"]! as! Int
let Minute: Int = data["Minute"]! as! Int

FHour = Hour
FMinute = Minute

//TODO:- Use other firebase related task same as your code, so that uncomment other lines same as your origional

then(FHour, FMinute) // This is closure call back line

}
}

//Calling your function
getTime { (first, second) in

print(first,second)

}

关于Swift 4 函数对于多个返回值返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47298666/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com