Both ways are correct, but sometimes you should use the initialization in init()
method. For example, here the target
for barButton will be not set, cause the self
does not exist yet.
class Foo {
var barButton = UIBarButtonItem(title: "add", style: .Plain, target: self, action: #selector(self.someMethod))
init(){
//init here
}
}
The correct way for this case is :
class Foo {
var barButton : UIBarButton?
init(){
barButton = UIBarButtonItem(title: "add", style: .Plain, target: self, action: #selector(self.someMethod))
}
}
To sum up, both ways are correct, but you have to consider when to use each one.
More information about it on Apple documentation