简体   繁体   English

如果其他条件正确,如何处理多个?

[英]How to handle multiple if else conditions properly?

I have three conditions: 我有三个条件:

  1. Both data.weight and myoutput are nil data.weightmyoutput均为nil
  2. myoutput has no value myoutput没有价值
  3. data.weight and myoutput both have a value data.weightmyoutput都有一个值

     override func viewWillAppear(_ animated: Bool) { super .viewWillAppear(animated) self.navigationItem.hidesBackButton = true let myOutput = UserDefaults.standard.string(forKey: "height") let dateFormator = DateFormatter() dateFormator.dateFormat = "dd.MM.yyyy" let data = WeightTracker.mr_findFirst(with: NSPredicate(format: "date == %@", dateFormator.string(from: Date()))) as? WeightTracker if myOutput == nil && data?.weight == nil { bmiLabel.text = "--" } else if myOutput == nil && data?.weight != nil { bmiLabel.text = "--" } else { let dateFormator = DateFormatter() dateFormator.dateFormat = "dd.MM.yyyy" if let data = WeightTracker.mr_findFirst(with: NSPredicate(format: "date == %@", dateFormator.string(from: Date()))) as? WeightTracker { let myOutput: AnyObject? = UserDefaults.standard.object(forKey: "height") as AnyObject var sum = (myOutput as! NSString).doubleValue / 1000 let total1 = sum * sum let total = Double(Int(data.weight!)!) / total1 let dye = total/100.00 bmiLabel.text = String(dye) } } } 

The sum should only be calculated for the third condition. 仅应针对第三个条件计算总和。 What is the proper way to handle this? 处理此问题的正确方法是什么?

if let myOutput = UserDefaults.standard.string(forKey: "height") as? NSString , let weight = data?.weight {
    let dateFormator = DateFormatter()
    dateFormator.dateFormat = "dd.MM.yyyy"
    if let data = WeightTracker.mr_findFirst(with: NSPredicate(format: "date == %@", dateFormator.string(from: Date()))) as? WeightTracker{
        var sum = myOutput.doubleValue   / 1000
        let total1 = sum * sum
        let total = Double(Int(weight)!) / total1
        let dye = total/100.00

        bmiLabel.text = String(dye)
    }
} else {
    bmiLabel.text = "--"
}

Why don't you go the other way around 你为什么不反过来

if myOutput != nil && data?.weight != nil {
    // Your logic here
}
else {
    bmiLabel.text = "--"
}

Or maybe replacing if with if-let altogether to provide safe unwrapping and to get rid of optional chaining and/or force unwrapping 或者,也许更换ifif-let一起为客户提供安全和展开摆脱可选的链接和/或展开

if let myOutput = myOutput, let weight = data?.weight {
    // Your logic here
}
else {
    bmiLabel.text = "--"
}

Or if you want to provide early escape , then you can even use guard-let 或者,如果您想提早逃脱 ,那么甚至可以使用guard-let

guard let myOutputUnwrapped = myOutput, let weight = data?.weight else {
    bmiLabel.text = "--"
    return 
}

    /* Your logic here */

The advantages of if-let and guard-let is that you already get an unwrapped value to work with so you don't have to worry about ! if-letguard-let的优点在于,您已经获得了可打包使用的价值,因此您不必担心! and ? ? with variables. 与变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM