Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如果一个对象在appDelegate中实例化,它会永远存在吗?_Ios_Dependency Injection_Appdelegate - Fatal编程技术网

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

Ios 如果一个对象在appDelegate中实例化,它会永远存在吗?,ios,dependency-injection,appdelegate,Ios,Dependency Injection,Appdelegate,我正在学习一个教程,它使用了作者所说的一个称为反向依赖注入的术语。基本上,它是一个待办事项列表应用程序,可以实例化一个ItemStore对象,该对象跟踪所有待办事项、重新排列等。在本教程中,我们在AppDelegate中实例化了ItemStore对象,该对象通过UITableViewController的unwrapped属性在UITableViewController中访问。我的问题是,如果ItemStore对象在AppDelegate中实例化,它是否在应用的整个生命周期中都有效?如果是这样的

我正在学习一个教程,它使用了作者所说的一个称为反向依赖注入的术语。基本上,它是一个待办事项列表应用程序,可以实例化一个ItemStore对象,该对象跟踪所有待办事项、重新排列等。在本教程中,我们在AppDelegate中实例化了ItemStore对象,该对象通过UITableViewController的unwrapped属性在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:.
    }


}

@约金已经给出了一个很好的答案。如果将
变量声明为class
属性,我们可以从任何类访问它。
AppDelegate
类有一个
singleton
对象。我们可以通过这种方式访问它

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

因此,它总是返回具有相同数据的同一对象,并且您可以从您想要的任何类访问它,但方法中的变量只能通过此方法访问。

类成员的寿命与其所属对象的寿命相同,方法中的变量只有在方法执行时才有效。@JoakimDanielson所以我想你说的是,当appDelegate的方法ApplicationIdentinterBackground通过将应用放在后台激发时,这个ItemStore对象将不再存在?正确。它仅存在于应用程序中:didFinishLaunchingWithOptions:如果它与您在应用程序中创建的实例相同:didFinishLaunchingWithOptions:则是,但如果您创建了新的视图控制器,则不存在。我认为您的意思是依赖项注入,而不是委派,建议使用。现在,Rumy向您展示了如何访问应用程序委派从任何地方:不要这样做。这违反了法律。在初始化过程中或通过setter传递任何依赖项。