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 requires an init value for those var and I can do so in many different ways, but how should I choose between these ways?
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.
}
}
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.