gpt4 book ai didi

swift - 函数不返回字符串

转载 作者:行者123 更新时间:2023-11-28 09:58:30 25 4
gpt4 key购买 nike

所以我要重新开始编程,但遇到了问题。当我在其中存储一个值时,我的函数没有返回值。你们能看看并指出我为什么会这样吗?

func getLocation() -> NSString {
manager = OneShotLocationManager()
var tempLocation: NSString = "" // created an empty string for the var

manager!.fetchWithCompletion {location, error in
if let locatie = location {
tempLocation = String(locatie.coordinate.latitude) + "," + String(locatie.coordinate.longitude)
print(tempLocation) // It stores a value here but will not show it on the return
} else if let err = error {
tempLocation = err.localizedDescription
}
self.manager = nil
}
return tempLocation // It's not returning anything here..
}

最佳答案

退出函数后开始完成,所以我猜这就是问题所在。你返回 ""然后在完成代码中做一些事情

关于swift - 函数不返回字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33002198/

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