如何使用Optionals来停止Swift中的编译器错误 [英] How to use Optionals to stop a compiler error in Swift

查看:94
本文介绍了如何使用Optionals来停止Swift中的编译器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一种方法来检测字符串中的输入错误并通知用户。
采用以下示例:

I want a way to detect input errors in a string and notify the user. Take the following example:

    let fraction            = "15/8"

    let fractionArray       = fraction.components(separatedBy: "/")
    let numerator           = Double(fractionArray[0])
    let denominator         = Double(fractionArray[1])
    var linearFactor        = numerator! / denominator!

    print(numerator!, "/", denominator!, " = ", linearFactor)

但是如果我强制解包,字符串中的无效字符将强制编译错误,我宁愿通知用户输入字符串包含无效的分数。可选链接似乎是要走的路,但我无法正确使用语法。

But if I force unwrap, invalid characters in the string will force a compile error and I’d rather notify the user that the input string contains an invalid fraction. Optional chaining seems like the way to go but I can’t get the syntax right.

在我的代码(下面)中,我将可选的链接运算符放在数组旁边,包括 fraction? .components(separatedBy:/)但是Fix-it告诉我删除它。

In my code (below), I place the optional chaining operator next to the array as shown including fraction?.components(separatedBy: "/") but Fix-it tells me to delete it.

如果有一种比可选链接更好的方法来解决这个问题,有人可以解释一下我在搜索答案时可能错过的内容所以我可以使代码工作?谢谢

If there is a better way than optional chaining to address this problem can someone please explain what I might have missed when I searched for answers here so I can make the code work ? Thanks

    let fraction            = "15/8"

    if let fractionArray    = fraction?.components(separatedBy: "/") {
    let numerator           = Double(fractionArray[0])
    let denominator         = Double(fractionArray[1])
    var linearFactor        = numerator / denominator

    print(numerator, "/", denominator, " = ", linearFactor)
        } else {
            print("Invalid. Re-enter fraction")
        }


推荐答案


  1. 分数是字符串字符串?所以你不必使用分数?

  2. 组件返回 [] 而不是 [] ?,所以你可以使用fractionArray而无需解开任何东西

  3. 你唯一要解开的就是分子分母 ,他们的类型是 Double?

  4. 谢谢@OOPer,笑uld check denominator!= 0

  5. 谢谢@Martin R,应该检查fractionArray.count == 2

  1. fraction is String not String? so you don't have to use fraction?
  2. components return [] not []?, so you can use fractionArray without unwrap anything
  3. the only thing you have to unwrap is numerator and denominator, their type is Double?
  4. Thanks @OOPer, should check denominator != 0
  5. Thanks @Martin R, should check fractionArray.count == 2

所以我将重构以下代码:

so I'll refactor to the following code:

let fraction = "15/8"
let fractionArray = fraction.components(separatedBy: "/")
guard let numerator = Double(fractionArray[0]), 
    let denominator = Double(fractionArray[1]),
    denominator != 0,
    fractionArray.count == 2 else {
    print("Invalid. Re-enter fraction, or denominator == 0, or fractionArray.count != 2")
    return
}
let linearFactor = numerator / denominator
print(numerator, "/", denominator, " = ", linearFactor)

这篇关于如何使用Optionals来停止Swift中的编译器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆