我正在使用天气API中的SwiftyJson解析json数据。我有一个可选数据列表,我将它添加到单个保护语句中,使代码看起来更简单,更有效。但遗憾的是,在可选项列表中,我有时会从已解析的API数据中获取nil值,因此该语句将转到else并且不返回任何内容。我是否必须将guard else语句放到每个可选项中,或者有一种方法可以继续并在else语句中返回找到的非零值?
这是我的代码:
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
}
以下是解析更改故事板上标签的数据后的代码。
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.
}
答案 0 :(得分:0)
将标签text
设置为nil或Optional字符串是合法的。因此,对于每个项目,使用可选链接打开它并设置相应的标签text
。
不幸的是我不知道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
设置为一个可选的包装字符串并且不会造成任何损害。