How come when downcasting I don't have to use an initializer of ItemsTableViewController? Is it initialized?

99封情书 提交于 2019-12-13 08:08:43

问题


I'm working through a tutorial and I noticed that when downcasting I didn't have to use an initializer method of the object. Is the object initialized? In the AppDelegate codebase below, I'm referring to the ItemsTableViewController. All I had to do was say "as!" but didn't need to use an init method or double parenthesis like this "ItemsTableViewController()".

Is the ItemsTableViewController initialized and if so how?

//
//  AppDelegate.swift
//  HomepwnerThirdTime
//
//  Created by Laurence Wingo on 4/26/18.
//  Copyright © 2018 Laurence Wingo. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        //create an ItemStore when the application launches
        let itemStore = ItemStore()

        //create an ItemsTableViewController
        //set the window property which is of type UIWindow from the UIApplicationDelegate protocol and downcast this property to an initialized ItemsTableViewController by using as!
        let itemsController = window!.rootViewController as! ItemsTableViewController
        //now access the ItemsTableViewController and set its itemStore property since it is unwrapped in the ItemsTableViewController which needs to be set
        itemsController.itemStore = itemStore
        //we just set the itemStore property of the ItemsTableViewController!  Yayy, WHEW!! Now when the ItemsTableViewController is accessed with that unwrapped ItemStore object then it will be set!!! WHHHHEEEEWWWWW!
        return true
    }
}

回答1:


rootViewController sure is initialised. If it is not then it would have been nil, and casting it using as! would have caused an error. Here, by downcasting, you are not doing anything to the VC object. You are just telling Swift that "yes I'm sure this will be a ItemsTableViewController by the time the code is run, so don't worry about it".

How is the VC initialised then?

This has to do with how iOS handles the launching of an app. When you tap on an app, it opens and a UIWindow is created. Then the first VC in your storyboard is initialised and set as the rootViewController of the UIWindow. After doing all of that, your app delegate is called.

Note that when you are in the didFinishLaunching method, it's just that the VC has been created. The views in the VC are not loaded. That's what viewDidLoad is for.




回答2:


Casting and initialization have nothing to do with each other.

A cast is simply a way to tell the compiler: "Trust me, even though you think this object is one type, I know it really is another type".

Initialization is of course the creation of a new object.

In your code, your view controller has already been created for you through your storyboard. Your code is simply accessing this already created view controller. The cast is you telling the compiler that the rootViewController is actually an instance of ItemsTableViewController and not a plain old UIViewController.



来源:https://stackoverflow.com/questions/50088212/how-come-when-downcasting-i-dont-have-to-use-an-initializer-of-itemstableviewco

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!