如何避免强制解包变量? [英] How to avoid force unwrapping a variable?

查看:19
本文介绍了如何避免强制解包变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何避免使用 !执行强制解包的操作,因为使用它通常是一个糟糕的选择.

How do I avoid using the ! operation doing a force unwrap as using this is usually a bad option.

使用如下代码更好的选择是什么,使用它使代码看起来更简单,并且由于 if 检查变量!被调用永远不会为零,因此不会崩溃.

What is the better option with code like the following where using it makes the code look simpler and because of the if check the variable ! is called on will never be nil and so cannot crash.

我的导师向我们介绍了 bang(!) 运算符,然后告诉我们不要再使用它.当然,告诉我们为什么,如果 optional 为 nil,它会使我们的应用程序崩溃.

My instructor introduced us to the bang(!) operator and then told us to never use it again. Telling us why of course, that it will crash our app if the optional is nil.

然而,我发现自己处于这样的情况,其中 bang 运算符似乎是最简洁、最安全的选择.

However I find myself in situations like these where the bang operator seems to be the most concise and safe option.

func fullName() -> String {
    if middleName == nil {
        return "\(firstName) \(lastName)"
    }else{
        return "\(firstName) \(middleName!) \(lastName)"
    }
}

有没有更好的方法来做这样的事情?

Is there a better way to do something like this?

此外,如果有人想知道,这里是完整的课程.

Also, here's the full class if anybody is wondering.

class CPerson{
    var firstName: String
    var middleName: String?
    var lastName: String

    init(firstName: String, middleName: String?, lastName: String) {
        self.firstName = firstName
        self.middleName = middleName
        self.lastName = lastName
    }
    convenience init(firstName: String, lastName: String) {
        self.init(firstName: firstName, middleName: nil, lastName: lastName)
    }
    func fullName() -> String {
        if middleName == nil {
            return "\(firstName) \(lastName)"
        }else{
            return "\(firstName) \(middleName!) \(lastName)"
        }
    }
}

我的教练说如果我看到你使用 bang 运算符,我们就会打架"O_O

My instructor said "If I see you using the bang operator, we're going to fight" O_O

推荐答案

使用 if letguard 结构:

func fullName() -> String {
    if let middleName = middleName {
        return "\(firstName) \(middleName) \(lastName)"

    } else {
        return "\(firstName) \(lastName)"
    }
}

func fullName() -> String {
    guard let middleName = middleName else {
        return "\(firstName) \(lastName)"
    }
    return "\(firstName) \(middleName) \(lastName)"
}

为了完整性,我已经将 guard 语句放入其中,但正如其他人评论的那样,这在错误/失败情况下更常用.

I've put the guard statement in for completeness but as others have commented this is more commonly used in an error/failure case.

我还建议不要对字符串使用字符串插值.它们已经是字符串,不需要在新字符串中使用每个名称的description.

I would also advise against using string interpolation for Strings. They are already strings, there is no need to use the description of each name in a new string.

考虑return firstName + " " + lastName.请参阅 Swift 中字符串插值和字符串初始值设定项之间的区别,了解字符串插值可能返回意外结果的情况.

Consider return firstName + " " + lastName. See Difference between String interpolation and String initializer in Swift for cases when string interpolation could return an unexpected result.

这篇关于如何避免强制解包变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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