Swift avoids code repeating in the initializer

How can I avoid repeating code between my initializers? I want dateFormatter to remain let constant.

let dateFormatter: NSDateFormatter init() { dateFormatter = NSDateFormatter() dateFormatter.dateStyle = .MediumStyle dateFormatter.timeStyle = .MediumStyle super.init(nibName: nil, bundle: nil) } required init?(coder aDecoder: NSCoder) { dateFormatter = NSDateFormatter() dateFormatter.dateStyle = .MediumStyle dateFormatter.timeStyle = .MediumStyle super.init(coder: aDecoder) } override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) { dateFormatter = NSDateFormatter() dateFormatter.dateStyle = .MediumStyle dateFormatter.timeStyle = .MediumStyle super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) } 
+5
source share
4 answers

If this is true , the actual code will replace all the code with

 lazy var dateFormatter : NSDateFormatter = { let formatter = NSDateFormatter() formatter.dateStyle = .MediumStyle formatter.timeStyle = .MediumStyle return formatter }() 

This variable is lazily initialized once the first time it is accessed.

Edit: You can also declare a variable a constant without a lazy attribute.

 let dateFormatter : NSDateFormatter = { ... 

The difference is that formatting is created immediately ( not lazily) during instance initialization.

Edit:

In Swift 3, NSDateFormatter been renamed DateFormatter
and .MediumStyle to .MediumStyle

+9
source

In this case, you can set the default property value for formatting the date using closure .

Then the declaration of your property will look like this:

 let dateFormatter: NSDateFormatter = { let dateFormatter = NSDateFormatter() dateFormatter.dateStyle = .MediumStyle dateFormatter.timeStyle = .MediumStyle return dateFormatter }() 

and you can remove the date formatting bits from your initializers.

+3
source

This is the only way I could think of:

 class Sample: UIViewController { let dateFormatter: NSDateFormatter = NSDateFormatter() init() { super.init(nibName: nil, bundle: nil) setupDateFormatter() } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) setupDateFormatter() } override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) { super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) setupDateFormatter() } private func setupDateFormatter() { dateFormatter.dateStyle = .MediumStyle dateFormatter.timeStyle = .MediumStyle } } 
0
source

If you need to set up a property, I would go on the vaad path. But your installation code requires some procedure, for example, preparing a complex structure or preparing image caches, then I like to go for the lazy trick with closing. This closure of the installation is done only once, just like any other lazy var properties are initialized once. Therefore, here you can write quite complex procedural code. Remember to add private , then your subclasses will not interfere with your business.

 class MyViewController: NSViewController { init() { super.init(nibName: nil, bundle: nil)! self.setup() } override init?(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) { super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) self.setup() } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) self.setup() } override func encodeWithCoder(aCoder: NSCoder) { super.encodeWithCoder(aCoder) } lazy private var setup: ()->() = { // do some complex procedure here!! return {} }() } 
0
source

All Articles