现在的位置: 首页 > 综合 > 正文

使用NSKeyedArchiver保存数据

2013年09月06日 ⁄ 综合 ⁄ 共 4657字 ⁄ 字号 评论关闭

 

- (NSMutableArray *)loadMarkersFromFilePath:(NSString *)filePath {
    NSMutableArray *markers = nil;
    if (filePath == nil || [filePath length] == 0 ||
        [[NSFileManager defaultManager] fileExistsAtPath:filePath] == NO) {
        markers = [[[NSMutableArray alloc] init] autorelease];
    } else {
        NSData *data = [[NSData alloc] initWithContentsOfFile:filePath];
        NSKeyedUnarchiver *vdUnarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
        markers = [vdUnarchiver decodeObjectForKey:kSaveKeyMarkerLines];
        [vdUnarchiver finishDecoding];
        [vdUnarchiver release];
        [data release];
    }
    return markers;

}

- (void)saveMarkers:(NSMutableArray *)markers toFilePath:(NSString *)filePath {
    NSMutableData *data = [[NSMutableData alloc] init];
    NSKeyedArchiver *vdArchiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    [vdArchiver encodeObject:markers forKey:kSaveKeyMarkerLines];
    [vdArchiver finishEncoding];
    [data writeToFile:filePath atomically:YES];
    [vdArchiver release];
    [data release];

}

==================================================================================

[NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:nil];

-----------------------------------------------------------------------------------------------------------------------------------------------

  

NSString *str = @"abc";  

NSString *astr = @"efg";  

NSArray *Array = [NSArray arrayWithObjects:str, astr, nil];  

   

//Save  

NSString *Path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *filename = [Path stringByAppendingPathComponent:@"test"];  

[NSKeyedArchiver archiveRootObject:Array toFile:filename];  

   

10 str = @"a";  

11 astr = @"";  

12    

13 //load  

14 NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile: filename];  

15 str = [arr objectAtIndex:0];  

16 astr =  [arr objectAtIndex:1];  

17    

18 NSLog(@"str:%@",str);  

19 NSLog(@"astr:%@",astr);  

20    

21    

22    

23   

24 NSString *str = @"abc";  

25 NSString *astr = @"efg";  

26 NSArray *Array = [NSArray arrayWithObjects:str, astr, nil];  

27    

28 //Save  

29 NSUserDefaults *SaveDefaults = [NSUserDefaults standardUserDefaults];  

30 [SaveDefaults setObject:Array forKey:@"SaveKey"];  

31    

32 str = @"a";  

33 astr = @"";  

34    

35 //load  

36 Array = [SaveDefaults objectForKey:@"SaveKey"];  

37 str = [Array objectAtIndex:0];  

38 astr = [Array objectAtIndex:1];  

39 NSLog(@"str:%@",str);  

40 NSLog(@"astr:%@",astr);  

41    

42    

43    

44    

45   

46 NSString *str = @"abc";  

47 NSString *astr = @"efg";  

48 NSArray *Array = [NSArray arrayWithObjects:str, astr, nil];  

49    

50 //Save  

51 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  

52 NSString *documentsDirectory = [paths objectAtIndex:0];  

53 if (!documentsDirectory) {  

54     NSLog(@"Documents directory not found!");  

55 }  

56 NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"Savedatas.plist"];  

57 [[NSArray arrayWithObjects:Array,nil] writeToFile:appFile atomically:NO];      

58    

59    

60 //load  

61 if([[NSFileManager defaultManager] fileExistsAtPath:appFile])  

62     self.SaveDataArray = [NSMutableArray arrayWithContentsOfFile:appFile];          

63 else  

64     self.SaveDataArray = [NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Savedatas" ofType:@"plist"]];  

65 NSArray *strArray = [self.SaveDataArray objectAtIndex:0];  

66    

67 str = [strArray objectAtIndex:0];  

68 astr = [strArray objectAtIndex:1];  

69    

70    

71    

72    

73    

74 //坛子里的,搬过来。。。。。  

75 -(BOOL) writeApplicationData:(NSDictionary *)data  writeFileName:(NSString *)fileName  

76 {  

77     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  

78     NSString *documentsDirectory = [paths objectAtIndex:0];  

79     if (!documentsDirectory) {  

80         NSLog(@"Documents directory not found!");  

81         return NO;  

82     }  

83     NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];  

84     return ([data writeToFile:appFile atomically:YES]);  

85 }  

86    

87 -(id) readApplicationData:(NSString *)fileName  

88 {  

89     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  

90     NSString *documentsDirectory = [paths objectAtIndex:0];  

91     NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];  

92     NSDictionary *myData = [[[NSDictionary alloc] initWithContentsOfFile:appFile] autorelease];  

93     return myData;  

}

抱歉!评论已关闭.