What is the purpose of the following line of code? It doesn't seem to do anything. If it were in Playground or in a print statement it would at least show something.
_ = masterVC.view
It's in Apple's sample code at Sharing CloudKit Data with Other iCloud Users.
Here's the complete significant code that contains that line of code:
if let masterVC = masterNC?.viewControllers.first as? ZoneViewController {
_ = masterVC.view
start ? masterVC.spinner.startAnimating() : masterVC.spinner.stopAnimating()
}
Let's see the doc of
viewfromUIViewController:Seeing the project code:
masterVC.spinner, is alazy varthat inviewDidLoad()will be added as a subview of itsUITableView.Why do this then?
Because when you do:
Its
IBOutletand itsviewhasn't been loaded yet. You can reproduce it with getting having aIBOutleton that VC, or some subview that will be added to theviewinviewDidLoad(). Try to access the property, it will benilfor theIBOutlet(and usually crash with if it's declared forced unwrapped), andsubview.superviewwill be nil for the other one.That's also why when you use
UIStoryboardSegue, inprepare(for:sender:), when you do:There, it's rather common to pass the model (here a simple
String) instead of setting the value of theUILabel.I personally prefer calling
loadViewIfNeeded()instead, in my opinion it's less strange that invoking theviewand ignore the returned value.