Initializing UIViewController Variables

I am learning the swift language, and I have doubts about the initialization of variables in the 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 requires an init value for these var, and I can do it in different ways, but how to choose one of these methods?

I can initialize the inline variables:

 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 initialize variables that override 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 initialize 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. } } 

I can initialize 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 and not another? Is there a typical model or some kind of standard regarding this problem? Perhaps some of these solutions are cleaner than others, or even more efficient. Please help me understand the difference between the two. Thanks in advance.

+7
variables initialization ios uiviewcontroller swift
source share
1 answer

This question can be summed up to "When should additional options be used?". There are many great articles and documentation on this issue, but I will try to combine my experience with it, as well as the documentation and articles that I read.

While options have very specific functionality when used, I would 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 

This means that we should never anticipate that myVar assigned, and instead we should always expect both conditions, the first of which is that myVar matters, but it doesn’t. I assume that these things are related to the functionality that adds ? to the table. The compiler will not allow you to use myVar without unpacking it. Because of this, the compiler suggests (with every access to a property or function) to use this syntax:

 myVar?.myProperty = something 

Because of ? before . this line of code will check to see if myVar nil before myVar and executing the line of code. Thus, we expected and processed both conditions. This line of code will essentially be “ignored” if myVar is zero and executed if it is not.

This is different from another type of optional ! :

 myVar!.myProperty = something 

This will always try to unpack myVar . This line of code throws an exception saying that: "Suddenly, zero was found while expanding the value." Bye ? will fail.

If we change myVar declaration to use ! optional:

 var myVar:Class! = nil 

Then we can always use myVar without receiving a compiler error, saying that we need to deploy myVar before using it. For example, unlike another optional ( ? ), We can say:

 myVar.myProperty = something 

This line is equivalent to:

 myVar!.myProperty = something 

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

Output:

Using any of these options (or simply not using optional at all), we will inform the user about myVar things about myVar because of how the language forces you to or not to force you to deal with myVar .

? optional var myVar:Class? = nil var myVar:Class? = nil :

Should I use the option ? optional, we essentially force the user to always check nil .

! optional var myVar:Class! = nil var myVar:Class! = nil :

If we use ! , then if myVar is nil, something is wrong and we need to minimize the program, however, the user still has the ability to handle the nil case, which is especially useful if the user who was to assign myVar . The big advantage of this is network requests.

no optional var myVar = Class() :

Do not use optional with all means (obviously) that the variable is always , and we do not need to worry about that it is nil .

+7
source share

All Articles