title: How to run a function when an app first loads
body: <p>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.</p>
  
  <p>I've got the following code:</p>
  
  <pre><code>var itemList = [String]()
  
  func loadData() -&gt; [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")
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>Where is the best place to call this so that it will load once only, upon the app starting up?</p>