AlexanderHart
AlexanderHart

Reputation: 77

Is NSUserDefaults Not Efficient?

In my iOS app, I have static string data that is displayed across multiple view controllers during the app's run time. I am using NSUserDefaults at the moment to store the data locally for later access, but I am questioning if this is the most optimal way to do things.

Alternatively, what if I had secondViewController.swift

let nameText  = ""
let nameLabel = UILabel()

override func viewDidLoad() {
    super.viewDidLoad()

    nameLabel.text = nameText
}

and in firstViewController.swift I did this

override func viewDidLoad() {
    super.viewDidLoad()

    let secondVC = secondViewController(nibNamed: "secondViewController", bundle: nil)
    secondVC.nameText = "John Doe"
}

Would the above be most optimal in terms of keeping the data not stale?

Upvotes: 1

Views: 66

Answers (1)

Mark Bessey
Mark Bessey

Reputation: 19782

In general, NSUserDefaults is intended to be used to store small amounts of information between runs of your app, rather than as a way to transfer data between view controllers.

You might want to read up on the Model-View-Controller software design pattern, which is a way to get data out of your view controllers, and into a "model object", which can be shared by multiple view controllers.

Upvotes: 1

Related Questions