在 Xcode 8 和 iOS 10 中使用 NSUserDefaults
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37798426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using NSUserDefaults with Xcode 8 and iOS 10
提问by JAL
NSUserDefaults
no longer appears to be a class in the iOS 10 SDK:
NSUserDefaults
似乎不再是 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)
(这是一个规范的问答对,以防止重复问题的泛滥)
回答by JAL
NSUserDefaults
has been renamed to UserDefaults
. standardUserDefaults()
has been renamed to standard()
.
NSUserDefaults
已更名为UserDefaults
. standardUserDefaults()
已更名为standard()
.
let defaults = UserDefaults.standard
This now works.
这现在有效。
回答by Shrawan
To be more precise to use UserDefaults in Swift-3 :-
为了更精确地在 Swift-3 中使用 UserDefaults :-
//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)
回答by rickster
@JAL's answer is correct, but perhaps too specific.
@JAL 的回答是正确的,但可能太具体了。
Lotsof API got renamed in Swift 3. Most Foundation types (NSUserDefaults
among them) have both lost their NS
prefix and had their methods renamedto 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.
许多API 在 Swift 3 中被重命名。大多数 Foundation 类型(NSUserDefaults
其中)都失去了它们的NS
前缀,并且它们的方法被重命名以反映Swift 3 API Guidelines。基金会还“取代” *一帮其基本数据类型类(中NSURL
,NSIndexPath
,NSDate
与斯威夫特本地的,等等)值类型(URL
,IndexPath
,Date
等)。方法重命名也适用于您在应用中使用的任何其他 Cocoa / Cocoa Touch API。
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 3should be to choose Edit > Convert > To Current Swift Syntaxfrom 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).
一行一行地处理这些问题,肯定是发疯的办法。将项目移动到 Swift 3 时,您要做的第一件事应该是从菜单栏中选择Edit > Convert > To Current Swift Syntax。这将立即应用所有更改,包括一行代码受多个更改影响的情况(因此单独处理它们可能无法让您到达您认为要去的地方)。
*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
.
*我将“替换”放在引号中,因为NS
在您可能需要它们的情况下,相应的类仍然存在,但任何使用它们的 API 都引用新的值类型:例如,tableView(_:cellForRowAt:)
现在采用IndexPath
,而不是NSIndexPath
。