UIViewController变量初始化 [英] UIViewController variables initialization

查看:108
本文介绍了UIViewController变量初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 swift 语言,我对 UIViewController 中的变量初始化有疑问。在我的 DiagramViewController 中我有一些变量:

I'm studying the swift language and I have a doubt concerning the variables initialization in a UIViewController. In my DiagramViewController I have some variables:

class DiagramViewController: UIViewController {

    var type: Constants.DiagramType
    var filename: String
    var numberOfBars: Int
    var numberOfSection: Int
    var diagramName: String

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

Swift需要一个初始值var和我可以通过多种方式实现,但我应该如何在这些方式之间做出选择呢?

Swift requires an init value for those var and I can do so in many different ways, but how should I choose between these ways?

我可以初始化变量inline:

I can init the variables "inline":

class DiagramViewController: UIViewController {

    var type: Constants.DiagramType = Constants.DiagramType.HISTOGRAM
    var filename: String = "dd.txt"
    var numberOfBars: Int = 10
    var numberOfSection: Int = 5
    var diagramName: String = "Diagram"

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

我可以初始化变量覆盖构造函数:

I can init the variables overriding the constructor:

class DiagramViewController: UIViewController {

    var type: Constants.DiagramType
    var filename: String
    var numberOfBars: Int
    var numberOfSection: Int
    var diagramName: String

    required init(coder aDecoder: NSCoder) {
        type = Constants.DiagramType.HISTOGRAM
        filename = "dd.txt"
        numberOfBars = 10
        numberOfSection = 5
        diagramName = "Diagram"

        super.init(coder: aDecoder)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

我可以初始化变量,将它们声明为可选变量:

I can init the variables declaring them as Optional variables:

class DiagramViewController: UIViewController {

    var type: Constants.DiagramType?
    var filename: String?
    var numberOfBars: Int?
    var numberOfSection: Int?
    var diagramName: String?

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        type = Constants.DiagramType.HISTOGRAM
        filename = "dd.txt"
        numberOfBars = 10
        numberOfSection = 5
        diagramName = "Diagram"

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

我可以初始化变量,将它们声明为 Implicitly Unwrapped可选

I can init the variables declaring them as Implicitly Unwrapped Optional:

class DiagramViewController: UIViewController {

    var type: Constants.DiagramType!
    var filename: String!
    var numberOfBars: Int!
    var numberOfSection: Int!
    var diagramName: String!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        type = Constants.DiagramType.HISTOGRAM
        filename = "dd.txt"
        numberOfBars = 10
        numberOfSection = 5
        diagramName = "Diagram"

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

为什么选择方法而不是其他方法?有关此问题的典型模式还是某种标准?也许其中一些解决方案比其他解决方案更清洁,甚至更高效。请帮我理解它们之间的区别。先谢谢你。

Why choose a method rather than another? Is there a typical pattern or a sort of standard concerning this issue? Maybe some of these solutions are cleaner than the others or even more efficient. Please help me understanding the difference between them. Thank you in advance.

推荐答案

这个问题可归纳为何时应该使用选项?。关于这个问题有很多很棒的文章和文档,但我会尝试将我的经验与我读过的文档和文章结合起来。

This question could be summarized down to "When should I use optionals?". There are lots of great articles and documentation on this question, but I will attempt to put together my experience with it as well as the documentation and articles I have read.

虽然Optionals在使用时具有非常特定的功能,我想将它们更多地视为一种关于变量本身而不是声明功能的方式。当我读到:

While Optionals have very specific functionality when used, I'd like to think of them more as a way of saying something about the variable itself rather than declaring functionality. When I read:

var myVar:Class? = nil

这意味着我们永远不会预料到 myVar 已分配,而我们应始终预测这两个条件,第一个是 myVar 有一个值,而它没有。我假设这些东西是因为可选的功能带给了表。编译器不允许您在不解包的情况下使用 myVar 。因此,编译器建议(无论何时访问属性或函数)使用此语法:

This means, that we should never anticipate that myVar is assigned and instead we should always anticipate both conditions, the first being that myVar has a value, and that it doesn't. I assume these things because of the functionality that the ? optional brings to the table. The compiler will not allow you to use myVar without unwrapping it. Because of this, the compiler suggests (whenever you access a property or function) that you use this syntax:

myVar?.myProperty = something

由于之前这行代码将检查以查看 myVar 是否 myVar 并执行代码行之前,> nil 。因此,我们预测并处理了这两个条件。如果myVar为nil,则这行代码基本上会被忽略,如果不是则执行。

Because of the ? before the . this line of code will check to see if myVar is nil before unwrapping myVar and executing the line of code. Thus we have anticipated and handled both conditions. This line of code will essentially be "ignored" if myVar is nil and executed if it isn't.

这与其他类型的可选<$ c相反$ c>!:

myVar!.myProperty = something

始终尝试解包 myVar 。这行代码将导致异常,说明以下内容:在展开值时意外发现nil。。虽然将无声地失败。

That will always try to unwrap myVar. This line of code will cause an exception saying something to the effect of: "Unexpectedly found nil while unwrapping a value.". While the ? will fail silently.

如果我们更改 myVar的声明使用可选:

If we change the declaration of myVar to use the ! optional:

var myVar:Class! = nil

然后我们总是可以使用 myVar 没有得到编译器错误,说我们需要在使用之前解开 myVar 。例如,与其他可选()不同,我们可以说:

Then we can always use myVar without getting the compiler error saying that we need to unwrap myVar prior to using it. For example, unlike the other optional (?), we can say:

myVar.myProperty = something

此行相当于:

myVar!.myProperty = something

因此,如果 myVar nil ,那么我们将使程序崩溃。

So if myVar is nil, then we will crash the program.

使用其中一个选项(或根本不使用可选项)我们告诉用户 myVar 关于 myVar 的事情,因为该语言会强迫或不强迫您处理 myVar

Using either one of these optionals (or simply not using an optional at all) we are telling the user of myVar things about myVar because of the way the language will force or not force you to deal with myVar.

如果我使用可选,我们基本上是强制用户总是检查 nil

If I use the ? optional, we're essentially forcing the user to always check for nil.

如果我们使用那么如果 myVar 是nil,出了点问题我们应该让程序崩溃,但是,用户仍然可以选择处理 nil 的情况,特别是如果用户是应该分配 myVar 的用户,则非常有用。一个很好的用例是网络请求。

If we use ! then if myVar is nil, something is wrong and we should crash the program, however, the user still has the option to handle the nil case which is especially useful if the user is the one whom was supposed to assign myVar. A great use case of this is network requests.

在任何情况下都没有使用可选(显然)变量总是,我们不需要担心它是 nil

Not using an optional at all means (obviously) that the variable is always there and we don't ever need to worry that it is nil.

这篇关于UIViewController变量初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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