gpt4 book ai didi

swift - guard 声明变量不给我值

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

var country = ""
if placemark.country != nil{
country = placemark.country!
}
self.awareLocationDetaile.text = country

尝试转换这段代码保护语句

guard let country = placemark.country else{                        
return
}
self.awareLocationDetaile.text = country

但不幸的是,如果我使用 guard 语句 where if give me value ,不幸的国家没有值(value)。我错过了什么?

更新:

        CLGeocoder().reverseGeocodeLocation(location) { ( clPlacemark :[CLPlacemark]?,error : NSError?) in
if error != nil {
print(error)
}else{
if let placemark = clPlacemark?[0]{
// print(placemark)
//MARK:- we are a good programmer
// var country = ""
// if placemark.country != nil{
// country = placemark.country!
// }
// self.awareLocationDetaile.text = country


guard let country = placemark.country else{

return

}


self.awareLocationDetaile.text = country
}

}
}

最佳答案

guard 语句完全按照它应该的方式工作。你没有遗漏任何东西。它做它应该做的,而不是你想要的。您的第一个代码示例可以更容易地编写为

self.awareLocationDetaile.text = placemark.country ?? ""

您似乎复制了女王在她 89 岁高龄时写下第一行代码后写的评论。注意皇室“我们”:

//MARK:- we are a good programmer

另一方面,guard 语句中的 return 语句将从闭包中返回,所以之后的任何事情都不会被执行。

关于swift - guard 声明变量不给我值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39934046/

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