我从天气API使用SwiftyJson解析JSON数据。我有一个可选数据的列表,我将它添加到单个警戒语句中,使代码看起来更简单,更有效。但不幸的是,在可选项列表中,我有时从解析的API数据中获得零值,因此声明转到其他部分并且不返回任何结果。我是否必须将guard else语句放到每个可选语句中,或者有一种方法可以继续并在else语句中返回找到的非零值?任选项的警戒声明无法迅速
这里是我的代码:
这里是解析改变的故事板标签数据后的代码。
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.
}
如果其中一个可选项为零,您想要做什么?换句话说:如果你不想中止,为什么你首先测试所有这些值?你的'cTemp'等纯粹是局部变量,你没有对它们做任何事情,所以你的代码的目的是什么? – matt
我想忽略它并继续操作。在故事板标签上发布当前找到的数据。或者,也许我必须尝试把所有剩余的代码放在else语句中?但数据将不完整。因为它在检查守卫声明中找到的所有可选项之前正在返回。 –
好的,很酷。那么你在那里做的那个部分在哪里?如果你想帮助你重写你的代码,你需要显示你的代码的一部分。 – matt