如何在应用程序首次加载时运行函数

时间:2022-05-28 03:36:33

I'm trying to write a simple to-do list in Swift that will store the list as an array of Strings and then call it back from memory when the app loads.

我正在尝试在Swift中编写一个简单的待办事项列表,它将列表存储为字符串数组,然后在应用程序加载时从内存中调用它。

I've got the following code:

我有以下代码:

var itemList = [String]()

func loadData() -> [String] {
    var arr = [String]()

    if NSUserDefaults.standardUserDefaults().objectForKey("storedData") != nil {
        arr = NSUserDefaults.standardUserDefaults().objectForKey("storedData")! as! [String]
    }
    else {
        arr = ["Nothing to do..."]
    }

    return arr
}

func saveData(arr: [String]) {
    NSUserDefaults.standardUserDefaults().setObject(arr, forKey: "storedData")
}

Where I'm getting stuck is in where to place the call to loadData(). This is an app that has two view controllers (one for the list, one for an add item setup), so if I place the loadData() call in viewDidLoad() for the main view controller, the array is called back in from memory (and over-written) every time I switch back to the main view controller.

我遇到的问题是在何处调用loadData()。这是一个具有两个视图控制器的应用程序(一个用于列表,一个用于添加项目设置),因此如果我在主视图控制器的viewDidLoad()中放置loadData()调用,则从内存中调用该数组每次切换回主视图控制器时(并覆盖)。

Where is the best place to call this so that it will load once only, upon the app starting up?

在应用程序启动时,最好的位置调用它,以便它只加载一次?

4 个解决方案

#1


6  

the array is called back in from memory (and over-written) every time I switch back to the main view controller.

每次切换回主视图控制器时,数组都会从内存中调用(并覆盖)。

No. viewDidLoad only loads once, when the app starts. Only viewWillApprear and viewDidAppear get called everytime the viewcontroller changes.

编号viewDidLoad仅在应用启动时加载一次。每次viewcontroller更改时,都会调用viewWillApprear和viewDidAppear。

Also you could make your code a bit more compact by using if let:

此外,通过使用if let,您可以使代码更紧凑:

  if let storedData = NSUserDefaults.standardUserDefaults().objectForKey("storedData") as! [String]{
     arr = storedData
  }

But if you want to make sure to load this only once, you can put it in your AppDelegate file in your applicationDidFinishWithOptions method.

但是如果你想确保只加载一次,你可以将它放在applicationDidFinishWithOptions方法的AppDelegate文件中。

But you'd have to make a variable in your AppDelegate file which you can access from your viewController.

但是你必须在AppDelegate文件中创建一个可以从viewController访问的变量。

#2


0  

viewDidLoad() only happens when the View Controller is first instantiated. If it is your root view controller you can have it in viewDidLoad().

viewDidLoad()仅在首次实例化View Controller时发生。如果它是您的根视图控制器,您可以在viewDidLoad()中使用它。

The other goes, viewDidLoad > viewWillAppear > viewDidAppear. After the view is first loaded only the latter 2 methods are called whenever you navigate.

另一个是viewDidLoad> viewWillAppear> viewDidAppear。首次加载视图后,只要导航,就会调用后两种方法。

you can also always register for a NSApplicationDidFinishLaunchingNotification notification at the notification center

您还可以在通知中心注册NSApplicationDidFinishLaunchingNotification通知

check it out here: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSApplication_Class/index.html#//apple_ref/c/data/NSApplicationDidFinishLaunchingNotification

在这里查看:https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSApplication_Class/index.html#//apple_ref/c/data/NSApplicationDidFinishLaunchingNotification

#3


0  

Use or overwrite respectively

分别使用或覆盖

application(_:didFinishLaunchingWithOptions:)

of your application delegate. That is called only once upon application launch. See https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/#//apple_ref/occ/intfm/UIApplicationDelegate/application:didFinishLaunchingWithOptions:

你的申请代表。只在应用程序启动时调用一次。请参阅https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/#//apple_ref/occ/intfm/UIApplicationDelegate/application:didFinishLaunchingWithOptions:

#4


0  

viewDidLoad is called after the view for a single controller is first loaded. It shouldn't be called more than once for the life-cycle of a single viewController. Maybe it is possible that, if you are not calling "super.viewDidLoad()" in your own viewDidLoad method, then it may be called again? While you can generally assume that the rootViewController for an application is only created once, I think it's theoretically possible that it might be cleared out of memory by the app if required and then recreated again - so I would never assume it's only called once.

首次加载单个控制器的视图后调用viewDidLoad。对于单个viewController的生命周期,不应该多次调用它。也许有可能,如果你没有在自己的viewDidLoad方法中调用“super.viewDidLoad()”,那么可能会再次调用它?虽然你通常可以假设应用程序的rootViewController只创建一次,但我认为理论上可能的是,如果需要它可能被应用程序清除内存,然后再次重新创建 - 所以我永远不会认为它只被调用一次。

One thing you could do is just set a boolean (default false) to true whenever you load the data and then not call it again if the flag is already set to true.

你可以做的一件事就是只要加载数据就将boolean(默认值为false)设置为true,如果标志已设置为true,则不再调用它。

Alternatively, it's a good idea to separate the data management from your viewControllers. A relatively simple solution would be to have a class called "AppData" say, which might be a singleton (so you can only ever have one instance of it) or a member of your AppDelegate. Then, in your app delegate's "applicationDidFinishLaunchingWithOptions method, you could create the one instance of the AppData class and call the loadData method on it. This class would then live independently of whichever view is currently showing, and the current view could call methods on this object to load/save/update data as required.

或者,将数据管理与viewControllers分开是个好主意。一个相对简单的解决方案是让一个名为“AppData”的类说,它可能是一个单例(所以你只能拥有它的一个实例)或AppDelegate的一个成员。然后,在你的app delegate的“applicationDidFinishLaunchingWithOptions方法中,你可以创建一个AppData类的实例并在其上调用loadData方法。这个类将独立于当前显示的任何视图而生存,并且当前视图可以调用此方法根据需要加载/保存/更新数据的对象。

#1


6  

the array is called back in from memory (and over-written) every time I switch back to the main view controller.

每次切换回主视图控制器时,数组都会从内存中调用(并覆盖)。

No. viewDidLoad only loads once, when the app starts. Only viewWillApprear and viewDidAppear get called everytime the viewcontroller changes.

编号viewDidLoad仅在应用启动时加载一次。每次viewcontroller更改时,都会调用viewWillApprear和viewDidAppear。

Also you could make your code a bit more compact by using if let:

此外,通过使用if let,您可以使代码更紧凑:

  if let storedData = NSUserDefaults.standardUserDefaults().objectForKey("storedData") as! [String]{
     arr = storedData
  }

But if you want to make sure to load this only once, you can put it in your AppDelegate file in your applicationDidFinishWithOptions method.

但是如果你想确保只加载一次,你可以将它放在applicationDidFinishWithOptions方法的AppDelegate文件中。

But you'd have to make a variable in your AppDelegate file which you can access from your viewController.

但是你必须在AppDelegate文件中创建一个可以从viewController访问的变量。

#2


0  

viewDidLoad() only happens when the View Controller is first instantiated. If it is your root view controller you can have it in viewDidLoad().

viewDidLoad()仅在首次实例化View Controller时发生。如果它是您的根视图控制器,您可以在viewDidLoad()中使用它。

The other goes, viewDidLoad > viewWillAppear > viewDidAppear. After the view is first loaded only the latter 2 methods are called whenever you navigate.

另一个是viewDidLoad> viewWillAppear> viewDidAppear。首次加载视图后,只要导航,就会调用后两种方法。

you can also always register for a NSApplicationDidFinishLaunchingNotification notification at the notification center

您还可以在通知中心注册NSApplicationDidFinishLaunchingNotification通知

check it out here: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSApplication_Class/index.html#//apple_ref/c/data/NSApplicationDidFinishLaunchingNotification

在这里查看:https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSApplication_Class/index.html#//apple_ref/c/data/NSApplicationDidFinishLaunchingNotification

#3


0  

Use or overwrite respectively

分别使用或覆盖

application(_:didFinishLaunchingWithOptions:)

of your application delegate. That is called only once upon application launch. See https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/#//apple_ref/occ/intfm/UIApplicationDelegate/application:didFinishLaunchingWithOptions:

你的申请代表。只在应用程序启动时调用一次。请参阅https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/#//apple_ref/occ/intfm/UIApplicationDelegate/application:didFinishLaunchingWithOptions:

#4


0  

viewDidLoad is called after the view for a single controller is first loaded. It shouldn't be called more than once for the life-cycle of a single viewController. Maybe it is possible that, if you are not calling "super.viewDidLoad()" in your own viewDidLoad method, then it may be called again? While you can generally assume that the rootViewController for an application is only created once, I think it's theoretically possible that it might be cleared out of memory by the app if required and then recreated again - so I would never assume it's only called once.

首次加载单个控制器的视图后调用viewDidLoad。对于单个viewController的生命周期,不应该多次调用它。也许有可能,如果你没有在自己的viewDidLoad方法中调用“super.viewDidLoad()”,那么可能会再次调用它?虽然你通常可以假设应用程序的rootViewController只创建一次,但我认为理论上可能的是,如果需要它可能被应用程序清除内存,然后再次重新创建 - 所以我永远不会认为它只被调用一次。

One thing you could do is just set a boolean (default false) to true whenever you load the data and then not call it again if the flag is already set to true.

你可以做的一件事就是只要加载数据就将boolean(默认值为false)设置为true,如果标志已设置为true,则不再调用它。

Alternatively, it's a good idea to separate the data management from your viewControllers. A relatively simple solution would be to have a class called "AppData" say, which might be a singleton (so you can only ever have one instance of it) or a member of your AppDelegate. Then, in your app delegate's "applicationDidFinishLaunchingWithOptions method, you could create the one instance of the AppData class and call the loadData method on it. This class would then live independently of whichever view is currently showing, and the current view could call methods on this object to load/save/update data as required.

或者,将数据管理与viewControllers分开是个好主意。一个相对简单的解决方案是让一个名为“AppData”的类说,它可能是一个单例(所以你只能拥有它的一个实例)或AppDelegate的一个成员。然后,在你的app delegate的“applicationDidFinishLaunchingWithOptions方法中,你可以创建一个AppData类的实例并在其上调用loadData方法。这个类将独立于当前显示的任何视图而生存,并且当前视图可以调用此方法根据需要加载/保存/更新数据的对象。