如何保存字符串到NSUserDefaults?
当前回答
对于Swift,我创建了两个函数,它们将从首选项中保存和检索值。
这对你很有帮助。
//保存对象
static func setObject(value:AnyObject ,key:String)
{
let pref = NSUserDefaults.standardUserDefaults()
pref.setObject(value, forKey: key)
pref.synchronize()
}
static func getObject(key:String) -> AnyObject
{
let pref = NSUserDefaults.standardUserDefaults()
return pref.objectForKey(key)!
}
其他回答
就像这样:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// saving an NSString
[prefs setObject:@"TextToSave" forKey:@"keyToLookupString"];
然后检索:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting an NSString
NSString *myString = [prefs stringForKey:@"keyToLookupString"];
你应该看看苹果的NSUserDefaults类参考,也可以看看这个教程:iPhone编程教程-使用NSUserDefaults保存/检索数据
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// saving an NSString
[prefs setObject:@"TextToSave" forKey:@"keyToLookupString"];
// saving an NSInteger
[prefs setInteger:42 forKey:@"integerKey"];
// saving a Double
[prefs setDouble:3.1415 forKey:@"doubleKey"];
// saving a Float
[prefs setFloat:1.2345678 forKey:@"floatKey"];
// This is suggested to synch prefs, but is not needed (I didn't put it in my tut)
[prefs synchronize];
检索
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting an NSString
NSString *myString = [prefs stringForKey:@"keyToLookupString"];
// getting an NSInteger
NSInteger myInt = [prefs integerForKey:@"integerKey"];
// getting an Float
float myFloat = [prefs floatForKey:@"floatKey"];
-(void)saveToUserDefaults:(NSString*)string_to_store keys:(NSString *)key_for_the_String
{
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
if (standardUserDefaults) {
[standardUserDefaults setObject:string_to_store forKey:key_for_the_String];
[standardUserDefaults synchronize];
}
}
叫它:
[self saveToUserDefaults:@"string_to_store" : @"key_for_the_string"];
使用以下方法检索字符串:
NSString * stored_string = [[NSUserDefaults standardUserDefaults] stringforkey:key_for_the_String]
在Swift 4.0.3 Xcode 9
============set Data in UserDefaults =========
UserDefaults.standard.set(userName, forKey: "userName")
UserDefaults.standard.synchronize()
============Get Data in UserDefaults =========
let userName = UserDefaults.standard.string(forKey: "userName")
print(userName ?? "Gera")
userNameTextField.text = ""+userName!
下面是如何对Swift做同样的事情;
var valueToSave = "someValue"
NSUserDefaults.standardUserDefaults().setObject(valueToSave, forKey: "preferenceName")
以后把它拿回来;
if let savedValue = NSUserDefaults.standardUserDefaults().stringForKey("preferenceName") {
// Do something with savedValue
}
在Swift 3.0中
var valueToSave = "someValue"
UserDefaults.standard.set(valueToSave, forKey: "preferenceName")
if let savedValue = UserDefaults.standard.string(forKey: "preferenceName") {
}
推荐文章
- 如何精确地以毫秒为单位记录方法的执行时间?
- 在整个UIWindow中获取UIView的位置
- 如何解散ViewController在Swift?
- 保存字符串到NSUserDefaults?
- 如何将JSON字符串转换为字典?
- 什么是NSLocalizedString等效在Swift?
- 查看保存的NSUserDefaults的简单方法?
- 如何创建一个字符串的格式?
- 如何添加UITableViewCell之间的间距
- 电话:用于文本输入的数字键盘
- 在为设备编译时,Apple Mach-O连接器错误
- 获取最顶端的UIViewController
- 使用isKindOfClass与Swift
- SourceKitService终止
- 如何从iPhone应用程序发送邮件