I am learning iOS development with the Big iOS Book by Big Nerd Ranch. I decided to implement my applications in Swift. In one of their applications, they have the following code in Objective-C:
- (UIView *)headerView
{
if (!_headerView) {
[[NSBundle mainBundle] loadNibNamed:@"HeaderView" owner:self options:nil]
}
return _headerView;
}
Apple Swift Guide to "@IBOutlet":
When you declare an exit in Swift, the compiler automatically converts this type to a weak implicitly expanded optional and sets it to the initial value nil. In fact, the compiler replaces the name @IBOutlet var: Type with @IBOutlet weak var name: Type! = Nil.
Lazy loading Properties swift, . @IBOutlet, , .
№ 1 (): , AppDelegate.swift. , "IBOutlet" , "
@IBOutlet var headerView : UIView {
if !_headerView {
NSBundle.mainBundle().loadNibNamed("HeaderView", owner: self, options: nil)
}
return _headerView!
}
var _headerView : UIView? = nil
№ 2 (): "@lazy" "@IBOutlet" , "@lazy" , , "@IBOutlet" , # 1
№ 3 (?): , . : Lazy Swift. , , - , "@IBOutlet var headerView: UIView!= Nil", TableViewController, , "", TableViewController.
@IBOutlet var headerView : UIView
func loadHeaderView() {
if !headerView {
println("loaded HeaderView")
NSBundle.mainBundle().loadNibNamed("HeaderView", owner: self, options: nil)
}
}
override func viewDidLoad() {
super.viewDidLoad()
loadHeaderView()
tableView.tableHeaderView = headerView
}
, ?
viewDidLoad() ?