Laurence Wingo
Laurence Wingo

Reputation: 3952

Why do we override variables which were initially nil instead of simply setting them?

For example, in MainViewController which is a subclass of UIViewcontroller, I overrode the nib name variable and returned a string to satisfy the argument however in the appDelegate I was able to simply assign an object to the rootViewController variable without using the term override. I understand the keyword override means to rewrite a method which is inherited by the subclass however I'm not understanding it in this sense where we're using variables.

AppDelegate.swift code:

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    var mainViewController: MainViewController?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        //so as this function fires we need to put forth some effort.....from inside this function instantiate a view controller
        let mainViewController = MainViewController()

        //put the view of the ViewController on screen
        //mainViewController.show(mainViewController, sender: self)

        window?.backgroundColor = UIColor.purple


        window?.rootViewController = mainViewController


        //set the property to point to the viewController
        self.mainViewController = mainViewController


        return true
    }

    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

MainViewController.swift code:

import Foundation
import UIKit

class MainViewController: UIViewController {

    override var nibName: String? {
        return "MainViewController"
    }

}

Upvotes: 0

Views: 55

Answers (1)

Max Chuquimia
Max Chuquimia

Reputation: 7854

nibName is a read-only property - checking the public UIViewController interface shows:

open var nibName: String? { get }

It doesn't make sense for the nibName to be able to change during the lifecycle of your UIViewController instance, so having it as a read-only property ensures it's simply a form of configuration.

Upvotes: 3

Related Questions