在iOS中也有几种方式,一般常用的有以下四种形式:
1. NSKeyedArchiver(加密形式)
** 2. NSUserDefaults **
** 3. Write写入方式 **
** 4. SQLite3 **
首先介绍第一种保存方式:NSKeyedArchiver
//=================NSKeyedArchiver========================
NSString *saveStr1 = @ "我是" ;
NSString *saveStr2 = @ "数据" ;
NSArray *array = [NSArray arrayWithObjects:saveStr1, saveStr2, nil];
//----Save
//这一句是将路径和文件名合成文件完整路径
NSString *Path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filename = [Path stringByAppendingPathComponent:@
"saveDatatest" ];
[NSKeyedArchiver archiveRootObject:array toFile:filename];
//用于测试是否已经保存了数据
saveStr1 = @ "hhhhhhiiii" ;
saveStr2 =@ "mmmmmmiiii" ;
//----Load
array = [NSKeyedUnarchiver unarchiveObjectWithFile: filename];
saveStr1 = [array objectAtIndex:0];
saveStr2 = [array objectAtIndex:1];
CCLOG(@ "str:%@" ,saveStr1);
CCLOG(@ "astr:%@" ,saveStr2);
第二种保存方式:NSUserDefaults
在 applicationDidBecomeActive中添加以下代码:
[[NSUserDefaults standardUserDefaults] synchronize];
写到disk里面
//=================NSUserDefaults========================
NSString *saveStr1 = @ "我是" ;
NSString *saveStr2 = @ "数据" ;
NSArray *array = [NSArray arrayWithObjects:saveStr1, saveStr2, nil];
//Save
NSUserDefaults *saveDefaults = [NSUserDefaults standardUserDefaults];
[saveDefaults setObject:array forKey:@ "SaveKey" ];
//用于测试是否已经保存了数据
saveStr1 = @ "hhhhhhiiii" ;
saveStr2 =@ "mmmmmmiiii" ;
//---Load
array = [saveDefaults objectForKey:@ "SaveKey" ];
saveStr1 = [array objectAtIndex:0];
saveStr2 = [array objectAtIndex:1];
CCLOG(@ "str:%@" ,saveStr1);
CCLOG(@ "astr:%@" ,saveStr2);
第三种保存方式:Write写入方式
//=================Write写入方式========================
NSString *saveStr1 = @ "我是" ;
NSString *saveStr2 = @ "数据" ;
NSArray *array = [NSArray arrayWithObjects:saveStr1, saveStr2, nil];
//----Save
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
if (!documentsDirectory) {
NSLog(@ "没找到" );
}
NSMutableArray *saveDataArray=nil;
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@
"Savedatas.plist" ];
[[NSArray arrayWithObjects:array,nil] writeToFile:appFile atomically:NO];
//用于测试是否已经保存了数据
saveStr1 = @ "hhhhhhiiii" ;
saveStr2 =@ "mmmmmmiiii" ;
//----Load
if ([[NSFileManager defaultManager] fileExistsAtPath:appFile]){
saveDataArray = [NSMutableArray arrayWithContentsOfFile:appFile];
} else {
saveDataArray = [NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@
"Savedatas" ofType:@ "plist" ]];
}
NSArray *strArray = [saveDataArray objectAtIndex:0];
saveStr1 = [strArray objectAtIndex:0];
saveStr2 = [strArray objectAtIndex:1];
CCLOG(@ "str:%@" ,saveStr1);
CCLOG(@ "astr:%@" ,saveStr2);
对Write写入方式保存数据和读取数据封装了两个方法这里放出,给童鞋们行个方便:(其他两种大家封装起来风容易了,这里就不写了);
封装的函数如下:
//保存游戏数据
//参数介绍:
// (NSMutableArray *)data :保存的数据
// (NSString *)fileName :存储的文件名
-( BOOL
) saveGameData:(NSMutableArray *)data saveFileName:(NSString *)fileName
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
if (!documentsDirectory) {
NSLog(@ "Documents directory not found!" );
return NO;
}
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
return ([data writeToFile:appFile atomically:YES]);
}
//读取游戏数据
//参数介绍:
// (NSString *)fileName :需要读取数据的文件名
-(id) loadGameData:(NSString *)fileName
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
NSMutableArray *myData = [[[NSMutableArray alloc] initWithContentsOfFile:appFile] autorelease];
return myData;
}
1.对于取出数据的时候需要注意,例如如下代码:
NSUserDefaults *saveDefaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *arraySaveData =[saveDefaults objectForKey:@
"OhSaveData" ];
//NSMutableArray *arraySaveData=[NSMutableArray arrayWithArray:[saveDefaults objectForKey:@"OhSaveData"]];
第二句代码是通过一个文件名获取你存储的数据,返回数据数组,但是!一定要注意这里返回的数据数组是不可修改的!及时你将读取的数据赋给一个可修改的数组中也一样无法修改其中的数据,所以如果你想将取出的数据进行修改那么这里需要要使用第三行代码来获取,这里Himi将获取出的数据数组首先copy给了可修改数组中,那么此时你的可修改数组就可以正常修改了!
2.修改已经的存储文件;代码如下:
arraySaveData:表示新的数据数组;NSUserDefaults *saveDefaults = [NSUserDefaults standardUserDefaults]; [saveDefaults setObject:arraySaveData forKey:@ "已经存在的文件名"];
正确的NSUserDefaults默认参数设置方法
假设一个程序有一个默认参数 alert_message 它有一个默认值 @"This_is_my_default_alert_message" 当然,程序后期有可能会修改这个值。以前我开发时候总是这样,如果要用到这个值了就if([[NSUserDefaults standardUserDefaults] objectForKey:@"alert_message"]==nil){
[[NSUserDefaults standardUserDefaults] setObject:@"This_is_my_default_alert_message" forKey:@"alert_message"];
}
这样做一次确认后再做下面的动作,当然,这样做完全正确。但如果10是满分,这样做只能拿个9分,因为如果默认参数很多,这些代码就有可能分散到各个类里,1是管理麻烦,2是代码很glue.
正确的方法应该是把默认参数在applicationDidFinishLaunching时统一注册
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSDictionary *defaultValues = [NSDictionary dictionaryWithObjectsAndKeys:
@"This_is_my_default_alert_message", @"alert_message",
nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultValues];
[[NSUserDefaultsController sharedUserDefaultsController] setInitialValues:defaultValues];
}
这样做绝对会让程序代码优美很多。
转载地址:http://wiki.eoe.cn/page/iOS_blog_page_62131.html