如果在appDelegate中实例化一个对象,它会永远存在吗?

时间:2018-04-28 12:30:51

标签: ios dependency-injection appdelegate

我正在编写一个教程,该教程使用了一个名为逆依赖注入的术语,如作者所述。基本上它是一个待办事项列表应用程序,可以实例化一个ItemStore对象,跟踪所有待办事项,重新排列等。在本教程中,我们在AppDelegate中实例化ItemStore对象,该对象通过一个未包装的属性在UITableViewController中访问UITableViewController的。我的问题是,如果ItemStore对象在AppDelegate中实例化,它是否在应用程序的整个生命周期中存在?如果它确实如此,因为它不是单例,这意味着每次我在应用程序中显示UITableViewController是创建内存泄漏还是保留循环?

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        //create an ItemStore
        let itemStore = ItemStore()

        //now access the ItemsViewController and set its item store
        let itemsController = window!.rootViewController as! ItemsTableViewController
        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
    }

    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:.
    }


}

1 个答案:

答案 0 :(得分:1)

@Joakim已经给出了一个非常好的答案。如果声明为类variable,我们可以从任何类访问attributeAppDelegate类有一个singleton对象。我们可以通过这种方式访问​​它。

let appDelegate = UIApplication.shared.delegate as! AppDelegate
let aVariable = appDelegate.someVariable

因此它始终返回具有相同数据的同一对象,您可以从任何所需的类访问它,但方法中的变量只能通过此方法访问。