gpt4 book ai didi

json - 可选的守卫语句在 swift 中为零

转载 作者:行者123 更新时间:2023-11-30 13:39:17 25 4
gpt4 key购买 nike

我正在使用来自天气 API 的 SwiftyJson 解析 json 数据。我有一个可选数据列表,我将其添加到单个保护语句中,以使代码看起来更简单、更有效。但不幸的是,在选项列表中,我有时会从解析的 API 数据中得到 nil 值,因此该语句会转到 else 并不会返回任何内容。我是否必须对每个可选值添加保护 else 语句,或者有办法继续并返回 else 语句中找到的非 nil 值?

这是我的代码:

dispatch_async(dispatch_get_main_queue(), {
let jsonContent = JSON(data: data!)

guard let cTemp = jsonContent["currently"]["temperature"].double,
let cFeelsLike = jsonContent["currently"]["apparentTemperature"].double,
let cHumidity = jsonContent["currently"]["humidity"].double,
let cDewPoint = jsonContent["currently"]["dewPoint"].double,
let cPressure = jsonContent["currently"]["pressure"].double,
let cVisibility = jsonContent["currently"]["visibility"].double,
let cWindSpeed = jsonContent["currently"]["windSpeed"].double,
let cWindDirection = jsonContent["currently"]["windBearing"].double,
let cRainChance = jsonContent["currently"]["precipProbability"].double,
let cIconString = jsonContent["currently"]["icon"].string,
let cSummary = jsonContent["currently"]["summary"].string,
let cDailySummary = jsonContent["daily"]["summary"].string

else{

self.messageFrame.removeFromSuperview()
return
}

这是解析数据后更改 Storyboard上标签的代码。

 if self.segmentedControl.selectedSegmentIndex == 0 {
UIApplication.sharedApplication().applicationIconBadgeNumber = Int(round(cTemp))
self.tempLabel.text = String(Int(round(cTemp))) + "˚"
self.humidityLabel.text = String(Int(round(cHumidity*100))) + "%"
self.pressureLabel.text = String(Int(round(cPressure))) + NSLocalizedString(" mBar", comment: "milli Bar")
self.windSpeedLabel.text = String(Int(round(cWindSpeed))) + NSLocalizedString(" Km/h", comment: "Kilo fe El sa3a")
self.realFeelLabel.text = String(Int(round(cFeelsLike))) + "˚"
self.windDirectionLabel.text = self.windDirectionNotation(cWindDirection)
self.rainChanceLabel.text = String(Int(round(cRainChance * 100))) + "%"
// self.visibilityLabel.text = String(Int(round(cVisibility))) + NSLocalizedString(" Km", comment: "Km")
self.descriptionLabel.text = cSummary
self.descriptionMoreLabel.text = cDailySummary
self.bgImage.image = self.bgPicker(cIconString) //Change BG according to currently weather conditions.

} else {
self.tempLabel.text = String(Int(round(cTemp))) + "˚"
self.humidityLabel.text = String(Int(round(cHumidity*100))) + "%"
self.pressureLabel.text = String(Int(round(cPressure))) + NSLocalizedString(" mBar", comment: "milli Bar")
self.windSpeedLabel.text = String(Int(round(cWindSpeed))) + NSLocalizedString(" mph", comment: "meel fee el sa3a")
self.realFeelLabel.text = String(Int(round(cFeelsLike))) + "˚"
self.windDirectionLabel.text = self.windDirectionNotation(cWindDirection)
self.rainChanceLabel.text = String(Int(round(cRainChance * 100))) + "%"
// self.visibilityLabel.text = String(Int(round(cVisibility))) + NSLocalizedString(" mi", comment: "meel")
self.descriptionLabel.text = cSummary
self.descriptionMoreLabel.text = cDailySummary
self.bgImage.image = self.bgPicker(cIconString) //Change BG according to currently weather conditions.
}

最佳答案

将标签的 text 设置为 nil 或可选字符串是合法的。因此,对于每个项目,使用可选链将其展开并设置相应标签的文本

不幸的是,我不知道 SwiftyJSON,但如果这只是一个字典,您可以这样做:

// here is some test data
let content = ["currently":["temperature":"21"]]
let lab = UILabel()
// this is what you would do
lab.text = (content["currently"] as? NSDictionary)?["temperature"] as? String

重点是最后一行。如果我们得到nil,我们将标签的text设置为nil并且不会造成任何损害。如果我们得到了字符串,我们将标签的 text 设置为包装该字符串的可选值,并且不会造成任何损害。

关于json - 可选的守卫语句在 swift 中为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35754096/

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