2016-03-02 67 views
0

我从天气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. 
       } 
+0

如果其中一个可选项为零,您想要做什么?换句话说:如果你不想中止,为什么你首先测试所有这些值?你的'cTemp'等纯粹是局部变量,你没有对它们做任何事情,所以你的代码的目的是什么? – matt

+0

我想忽略它并继续操作。在故事板标签上发布当前找到的数据。或者,也许我必须尝试把所有剩余的代码放在else语句中?但数据将不完整。因为它在检查守卫声明中找到的所有可选项之前正在返回。 –

+0

好的,很酷。那么你在那里做的那个部分在哪里?如果你想帮助你重写你的代码,你需要显示你的代码的一部分。 – matt

回答

0

将标签的text设置为零或可选字符串是合法的。因此,对于每个项目,请使用可选链接将其打开并设置相应的标签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设置为一个可选的包装该字符串,并没有造成任何损害。

+0

确定您的意思是立即在每个可选陈述后更改标签?如果一个人没有伤害,他们是零。 –

+0

确切地说,但不幸的是,我无法确切地告诉你用SwiftJSON做这件事的语法。 – matt

+0

Hi @matt:这是SwiftyJSON的语法:'json [“current”] [“temperature”]。double'。这个库的美妙之处在于您可以安全地浏览JSON。如果您尝试将相关值转换为其中一种受支持的类型('Double','Int',...),则只访问不存在的键就会得到'nil'值。 –