1

I have a application that uses a files that corresponds with a view controller, however I want to include userdefaults (NSuserdefaults)in my program. Where would I create it as in the line

let defaults = NSUserDefaults.standardUserDefaults()

Where would I create the userdefaults? Would I put it in a separate file that a viewcontroller file for say a start scree? Or would I make a file just for the NSuserdefualt creation and call it in the other viewcontroller files?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • standardUserDefaults() is a singleton. It's created automatically by your application. – GetSwifty Apr 20 '17 at 22:34
  • 2
    What do you plan to use `UserDefaults` for? Only use it to store little bits of info such as user settings. Don't use it to store your app's data. – rmaddy Apr 20 '17 at 22:40
  • @rmaddy yes I do just for a one time term agreement thing jus to see if it was the first time the app was oppened – Joe Macksood Apr 20 '17 at 23:58
  • You should be using `UserDefaults` if you're using Swift instead of `NSUserDefaults`. – Daniel Storm Apr 21 '17 at 23:27

2 Answers2

0

NSUserDefaults

The NSUserDefaults class provides a programmatic interface for interacting with the defaults system. The defaults system allows an application to customize its behavior to match a user’s preferences. For example, you can allow users to determine what units of measurement your application displays or how often documents are automatically saved. Applications record such preferences by assigning values to a set of parameters in a user’s defaults database. The parameters are referred to as defaults since they’re commonly used to determine an application’s default state at startup or the way it acts by default.

You can just call it anywhere in your source code as it is a singleton class. You use this normally to store user settings.

You can refer to this answer, on how to use it in Swift 3 and Swift 2.3

Community
  • 1
  • 1
Christian Abella
  • 5,747
  • 2
  • 30
  • 42
  • Ok, but where do I create it? do I put it in a separate file? – Joe Macksood Apr 20 '17 at 23:59
  • no need for a file. Just use it. it will create the file automatically inside your app space. Just don't forget to call synchronize when you want to save the actual contents of the ncuserdefaults. – Christian Abella Apr 21 '17 at 00:02
0

You can put it anywhere really. Think of it as changing the value of a variable-where you can put that function.

Here's an example:

let highscore = 1000  //this is the value
//below is the saving part
let userDefaults = NSUserDefaults.standardUserDefaults()
userDefaults.setValue(highscore, forKey: "highscore")
userDefaults.synchronize()

I can put the saving part whenever I want to change and save the value. For example, you can put it in an Action of a button, in viewdidload, and more. Then typically you 'load' it again when you enter the app by putting the loading code (not included in this answer) in viewDidLoad, but you can also put it in a button action, function, whatever.

Andy Lebowitz
  • 1,471
  • 2
  • 16
  • 23