Using NSUserDefaults with Xcode 8 and iOS 10
Solution 1
NSUserDefaults
has been renamed to UserDefaults
. standardUserDefaults()
has been renamed to standard()
.
let defaults = UserDefaults.standard
This now works.
Pre-release documentation link.
Solution 2
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)
Solution 3
@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
.
JAL
iOS/tvOS video engineer living in San Francisco. You can find me in the SOCVR or in SOBotics as a room owner.
Updated on June 10, 2020Comments
-
JAL almost 4 years
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)
-
Andy Ibanez almost 8 yearsLooks like I'm gonna have some fun rewriting some past code.
-
JAL almost 8 yearsThanks rickster. There's a meta question up about the class renaming in iOS 10 here.
-
Joe over 7 years@JAL - once we make these NSUserDefault changes to conform to Swift 3 / ios10 / Xcode 8, will current values of all our NSUserDefaults persist??
-
JAL over 7 years@Joe Yes, all data will continue to persist. These are just class and function name changes.
-
Joe over 7 years@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?
-
JAL over 7 years@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.
-
Roland T. over 7 yearsNo need to write the
Foundation.
beforeUserDefaults