首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

封存数据的一系列方法

2012-09-04 
保存数据的一系列方法?/*NSKeyedArchiver

保存数据的一系列方法

?

/*=======================================================NSKeyedArchiver========================================================*/NSString *str = @"abc";NSString *astr = @"efg";NSArray *Array = [NSArray arrayWithObjects:str, astr, nil]; //SaveNSString *Path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *filename = [Path stringByAppendingPathComponent:@"test"];[NSKeyedArchiver archiveRootObject:Array toFile:filename]; str = @"a";astr = @""; //loadNSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile: filename];str = [arr objectAtIndex:0];astr =  [arr objectAtIndex:1]; NSLog(@"str:%@",str);NSLog(@"astr:%@",astr);   /*=======================================================NSUserDefaults========================================================*/NSString *str = @"abc";NSString *astr = @"efg";NSArray *Array = [NSArray arrayWithObjects:str, astr, nil]; //SaveNSUserDefaults *SaveDefaults = [NSUserDefaults standardUserDefaults];[SaveDefaults setObject:Array forKey:@"SaveKey"]; str = @"a";astr = @""; //loadArray = [SaveDefaults objectForKey:@"SaveKey"];str = [Array objectAtIndex:0];astr = [Array objectAtIndex:1];NSLog(@"str:%@",str);NSLog(@"astr:%@",astr);    /*=======================================================writeToFile:========================================================*/NSString *str = @"abc";NSString *astr = @"efg";NSArray *Array = [NSArray arrayWithObjects:str, astr, nil]; //SaveNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *documentsDirectory = [paths objectAtIndex:0];if (!documentsDirectory) {    NSLog(@"Documents directory not found!");}NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"Savedatas.plist"];[[NSArray arrayWithObjects:Array,nil] writeToFile:appFile atomically:NO];      //loadif([[NSFileManager defaultManager] fileExistsAtPath:appFile])    self.SaveDataArray = [NSMutableArray arrayWithContentsOfFile:appFile];        else    self.SaveDataArray = [NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Savedatas" ofType:@"plist"]];NSArray *strArray = [self.SaveDataArray objectAtIndex:0]; str = [strArray objectAtIndex:0];astr = [strArray objectAtIndex:1];     //坛子里的,搬过来。。。。。-(BOOL) writeApplicationData:(NSDictionary *)data  writeFileName:(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]);} -(id) readApplicationData:(NSString *)fileName{    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    NSString *documentsDirectory = [paths objectAtIndex:0];    NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];    NSDictionary *myData = [[[NSDictionary alloc] initWithContentsOfFile:appFile] autorelease];    return myData;} 

热点排行