31

NSUserDefaults no longer appears to be a class in the iOS 10 SDK:

let defaults = NSUserDefaults.standardUserDefaults()

This fails to compile. Was this class removed?

(This is a canonical Q&A pair to prevent the flood of duplicate questions)

JAL
  • 41,701
  • 23
  • 172
  • 300

3 Answers3

46

NSUserDefaults has been renamed to UserDefaults. standardUserDefaults() has been renamed to standard().

let defaults = UserDefaults.standard

This now works.

Pre-release documentation link.

JAL
  • 41,701
  • 23
  • 172
  • 300
  • 6
    Looks like I'm gonna have some fun rewriting some past code. – Andy Ibanez Jun 13 '16 at 22:29
  • @JAL - once we make these NSUserDefault changes to conform to Swift 3 / ios10 / Xcode 8, will current values of all our NSUserDefaults persist?? – Joe Sep 12 '16 at 05:36
  • @Joe Yes, all data will continue to persist. These are just class and function name changes. – JAL Sep 12 '16 at 05:39
  • @JAL - whew. Thanks! While on the subject - when Xcode 8 gets released publicly, and we all update our syntax (as discussed in your answer above), how does (will?) that affect users using your app that are still on ios8 & 9? – Joe Sep 12 '16 at 05:45
  • 1
    @Joe The syntax change should not affect users. This is purely a change to the Swift language. Any classes that are available on iOS 8 and 9 will still continue to be available. If you build an iOS 8 app with Swift 3 and Xcode 8, it should work the same as an iOS 10 app with Swift 2.3, an iOS 9 app built with Swift 2.2, etc. – JAL Sep 12 '16 at 05:47
20

To be more precise to use UserDefaults in Swift-3 :-

       //To save the string 
       let userDefaults = UserDefaults.standard
       userDefaults.set( "String", forKey: "Key")

       //To retrieve from the key
       let userDefaults = UserDefaults.standard
       let value  = userDefaults.string(forKey: "Key")
       print(value)
heinst
  • 8,520
  • 7
  • 41
  • 77
Shrawan
  • 7,128
  • 4
  • 29
  • 40
16

@JAL's answer is correct, but perhaps too specific.

Lots of API got renamed in Swift 3. Most Foundation types (NSUserDefaults among them) have both lost their NS prefix and had their methods renamed to reflect Swift 3 API Guidelines. Foundation also "replaces"* a bunch of its basic data type classes (NSURL, NSIndexPath, NSDate, etc) with Swift-native value types (URL, IndexPath, Date, etc). The method renaming also applies to any other Cocoa / Cocoa Touch APIs you use in your app.

Dealing with these issues one by one, line by line, is a sure way to madness. The first thing you do when moving a project to Swift 3 should be to choose Edit > Convert > To Current Swift Syntax from the menu bar. This will apply all the changes at once, including cases where one line of code is affected by multiple changes (and thus addressing them individually might not get you where you think you're going).

*I put "replaces" in quotes because the corresponding NS classes are still around for the cases where you might need them, but any API that uses them refers to the new value types instead: e.g. tableView(_:cellForRowAt:) now takes an IndexPath, not an NSIndexPath.

rickster
  • 124,678
  • 26
  • 272
  • 326
  • Thanks rickster. There's a meta question up about the class renaming in iOS 10 [here](http://meta.stackoverflow.com/q/326078/2415822). – JAL Jun 14 '16 at 05:50