So total noob to Swift/programming so excuse the seemingly obvious question; Could someone explain to me the functionality of the super prefix? Does it allow for the override to retain the functionality of the parent class? The tutorial I am working on tells me its used to let the parent class know when the child class is called? Not sure i fully understand this. Thanks in advance for the help!
import UIKit
class ViewController: UITableViewController {
var pictures = [String]()
override func viewDidLoad() {
super.viewDidLoad()
let fm = FileManager.default
let path = Bundle.main.resourcePath!
let items = try! fm.contentsOfDirectory(atPath: path)
for item in items {
if item.hasPrefix("nssl") {
pictures.append(item)
}
}
}
}
Imagine that UITableViewController
did some stuff in viewDidLoad
that was necessary for its operation, much like how your view controller does. When you override the inherited method, yours gets called instead of the super class's. Now you have a chance to do your initialization, but you've deprived the super class of the opportunity to do its initialization.
To remedy this, you would call the super implementation yourself. A lot of UIKit view cycle methods (view(Will|Did)(Load|Layout|Appear)
) don't strictly need to be called, because the super class implementations do nothing, but some do. Omitting them could lead to funky results, hence why it's usually good practice to make a habit out of calling the super class implementations of such methods.