The question title pretty much gives it away - I'd like my app to remember a few things. It's some sort of calculator, so it should save the last used values and some user selectable settings.
这个问题的标题很简单——我希望我的应用能记住一些东西。它是某种计算器,所以它应该保存最后使用的值和一些用户可选择的设置。
Basically I'd like to save a handful of floats and BOOLs and load them again the next time the app loads.
基本上,我想保存一些浮点数和BOOLs,下次应用加载时再加载它们。
What's the best and easiest way to do that?
最好、最简单的方法是什么?
Thanks!!
谢谢! !
5 个解决方案
#1
126
One of the easiest ways would be saving it in the NSUserDefaults
:
最简单的方法之一是在NSUserDefaults中保存它:
Setting:
设置:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:value
forKey:key];
// – setBool:forKey:
// – setFloat:forKey:
// in your case
[userDefaults synchronize];
Getting:
得到:
[[NSUserDefaults standardUserDefaults] objectForKey:key];
– boolForKey:
and
和
– floatForKey:
in your case.
- floatForKey:对你来说。
#2
6
Besides the very good NSUserDefaults approach, there is another easy way to store data from an NSArray,NSDictionary or NSData in a file. You can use these methods as well:
除了非常好的NSUserDefaults方法外,还有另一种简单的方法可以将来自NSArray、NSDictionary或NSData的数据存储到文件中。你也可以使用这些方法:
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag
respectively (for a NSDictionary):
分别(NSDictionary):
+ (id)dictionaryWithContentsOfFile:(NSString *)path
you just have to give a valid path to a location. According to the iOS Application Programming Guide, the /Library/Caches directory would be the best place to store data that you need to persist between app launches. (see here)
你只需要给一个位置一个有效的路径。根据iOS应用程序编程指南,/Library/ cache目录将是保存应用程序启动之间需要保存的数据的最佳位置。(见这里)
In order to store/load a dictionary from a filed called "managers" in your document directoy you could use these methods:
为了在文件directoy中存储/加载一个名为“managers”的文件字典,您可以使用以下方法:
-(void) loadDictionary {
//get the documents directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES);
NSString *cacheDirectory = [paths objectAtIndex:0];
//create a destination file name to write the data :
NSString *fullFileName = [NSString stringWithFormat:@"%@/managers", cacheDirectory];
NSDictionary* panelLibraryContent = [NSDictionary dictionaryWithContentsOfFile:fullFileName];
if (panelLibraryContent != nil) {
// load was successful do something with the data...
} else {
// error while loading the file
}
}
-(void) storeDictionary:(NSDictionary*) dictionaryToStore {
//get the documents directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cacheDirectory = [paths objectAtIndex:0];
//make a file name to write the data to using the
//cache directory:
NSString *fullFileName = [NSString stringWithFormat:@"%@/managers", cacheDirectory];
if (dictionaryToStore != nil) {
[dictionaryToStore writeToFile:fullFileName atomically:YES];
}
}
Anyway this approach is very limited and you have to spend a lot of extra work if you want to store more complex data. In that case the CoreData API is very very handy.
无论如何,这种方法是非常有限的,如果您想存储更复杂的数据,您必须花费大量额外的工作。在这种情况下,CoreData API非常方便。
#3
4
In Swift:
Setting
设置
let userDefaults = NSUserDefaults.standardUserDefaults()
userDefaults.setObject(value, forKey: key)
// userDefaults.setFloat(12.34, forKey: "myFloatKey")
// userDefaults.setBool(true, forKey: "myBoolKey")
Note that for iOS 8 and later, calling userDefaults.synchronize()
is not recommended.
注意,对于iOS 8和以后的版本,不建议调用userdefault .synchronize()。
Getting
得到
let userDefaults = NSUserDefaults.standardUserDefaults()
if let value = userDefaults.objectForKey(key) {
print(value)
}
Note that userDefaults.boolForKey
and userDefaults.floatForKey
both return non optional values, so they would never be nil
(only false
or 0.0
).
注意,userDefaults。boolForKey userDefaults。floatForKey都返回非可选值,因此它们永远不会是nil(只有false或0.0)。
Further reading
- NSUserDefaults — A Swift Introduction
- NSUserDefaults -快速介绍
#5
0
Swift 4 / Linux
斯威夫特4 / Linux
Apparently something has changed. Now there's the UserDefault
class. Check these links:
显然,有些事情已经改变了。现在有了UserDefault类。检查这些链接:
https://developer.apple.com/documentation/foundation/userdefaults
https://developer.apple.com/documentation/foundation/userdefaults
https://www.hackingwithswift.com/read/12/2/reading-and-writing-basics-userdefaults
https://www.hackingwithswift.com/read/12/2/reading-and-writing-basics-userdefaults
#1
126
One of the easiest ways would be saving it in the NSUserDefaults
:
最简单的方法之一是在NSUserDefaults中保存它:
Setting:
设置:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:value
forKey:key];
// – setBool:forKey:
// – setFloat:forKey:
// in your case
[userDefaults synchronize];
Getting:
得到:
[[NSUserDefaults standardUserDefaults] objectForKey:key];
– boolForKey:
and
和
– floatForKey:
in your case.
- floatForKey:对你来说。
#2
6
Besides the very good NSUserDefaults approach, there is another easy way to store data from an NSArray,NSDictionary or NSData in a file. You can use these methods as well:
除了非常好的NSUserDefaults方法外,还有另一种简单的方法可以将来自NSArray、NSDictionary或NSData的数据存储到文件中。你也可以使用这些方法:
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag
respectively (for a NSDictionary):
分别(NSDictionary):
+ (id)dictionaryWithContentsOfFile:(NSString *)path
you just have to give a valid path to a location. According to the iOS Application Programming Guide, the /Library/Caches directory would be the best place to store data that you need to persist between app launches. (see here)
你只需要给一个位置一个有效的路径。根据iOS应用程序编程指南,/Library/ cache目录将是保存应用程序启动之间需要保存的数据的最佳位置。(见这里)
In order to store/load a dictionary from a filed called "managers" in your document directoy you could use these methods:
为了在文件directoy中存储/加载一个名为“managers”的文件字典,您可以使用以下方法:
-(void) loadDictionary {
//get the documents directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES);
NSString *cacheDirectory = [paths objectAtIndex:0];
//create a destination file name to write the data :
NSString *fullFileName = [NSString stringWithFormat:@"%@/managers", cacheDirectory];
NSDictionary* panelLibraryContent = [NSDictionary dictionaryWithContentsOfFile:fullFileName];
if (panelLibraryContent != nil) {
// load was successful do something with the data...
} else {
// error while loading the file
}
}
-(void) storeDictionary:(NSDictionary*) dictionaryToStore {
//get the documents directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cacheDirectory = [paths objectAtIndex:0];
//make a file name to write the data to using the
//cache directory:
NSString *fullFileName = [NSString stringWithFormat:@"%@/managers", cacheDirectory];
if (dictionaryToStore != nil) {
[dictionaryToStore writeToFile:fullFileName atomically:YES];
}
}
Anyway this approach is very limited and you have to spend a lot of extra work if you want to store more complex data. In that case the CoreData API is very very handy.
无论如何,这种方法是非常有限的,如果您想存储更复杂的数据,您必须花费大量额外的工作。在这种情况下,CoreData API非常方便。
#3
4
In Swift:
Setting
设置
let userDefaults = NSUserDefaults.standardUserDefaults()
userDefaults.setObject(value, forKey: key)
// userDefaults.setFloat(12.34, forKey: "myFloatKey")
// userDefaults.setBool(true, forKey: "myBoolKey")
Note that for iOS 8 and later, calling userDefaults.synchronize()
is not recommended.
注意,对于iOS 8和以后的版本,不建议调用userdefault .synchronize()。
Getting
得到
let userDefaults = NSUserDefaults.standardUserDefaults()
if let value = userDefaults.objectForKey(key) {
print(value)
}
Note that userDefaults.boolForKey
and userDefaults.floatForKey
both return non optional values, so they would never be nil
(only false
or 0.0
).
注意,userDefaults。boolForKey userDefaults。floatForKey都返回非可选值,因此它们永远不会是nil(只有false或0.0)。
Further reading
- NSUserDefaults — A Swift Introduction
- NSUserDefaults -快速介绍
#4
#5
0
Swift 4 / Linux
斯威夫特4 / Linux
Apparently something has changed. Now there's the UserDefault
class. Check these links:
显然,有些事情已经改变了。现在有了UserDefault类。检查这些链接:
https://developer.apple.com/documentation/foundation/userdefaults
https://developer.apple.com/documentation/foundation/userdefaults
https://www.hackingwithswift.com/read/12/2/reading-and-writing-basics-userdefaults
https://www.hackingwithswift.com/read/12/2/reading-and-writing-basics-userdefaults