0
回答
保存数据的一系列方法
科大讯飞通用文字识别100000次/天免费使用。立即申请   
  1. /*=======================================================
  2. NSKeyedArchiver
  3. ========================================================*/
  4. NSString *str = @"abc";
  5. NSString *astr = @"efg";
  6. NSArray *Array = [NSArray arrayWithObjects:str, astr, nil];
  7. //Save
  8. NSString *Path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *filename = [Path stringByAppendingPathComponent:@"test"];
  9. [NSKeyedArchiver archiveRootObject:Array toFile:filename];
  10. str = @"a";
  11. astr = @"";
  12. //load
  13. NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile: filename];
  14. str = [arr objectAtIndex:0];
  15. astr =  [arr objectAtIndex:1];
  16. NSLog(@"str:%@",str);
  17. NSLog(@"astr:%@",astr);
  18. /*=======================================================
  19. NSUserDefaults
  20. ========================================================*/
  21. NSString *str = @"abc";
  22. NSString *astr = @"efg";
  23. NSArray *Array = [NSArray arrayWithObjects:str, astr, nil];
  24. //Save
  25. NSUserDefaults *SaveDefaults = [NSUserDefaults standardUserDefaults];
  26. [SaveDefaults setObject:Array forKey:@"SaveKey"];
  27. str = @"a";
  28. astr = @"";
  29. //load
  30. Array = [SaveDefaults objectForKey:@"SaveKey"];
  31. str = [Array objectAtIndex:0];
  32. astr = [Array objectAtIndex:1];
  33. NSLog(@"str:%@",str);
  34. NSLog(@"astr:%@",astr);
  35. /*=======================================================
  36. writeToFile:
  37. ========================================================*/
  38. NSString *str = @"abc";
  39. NSString *astr = @"efg";
  40. NSArray *Array = [NSArray arrayWithObjects:str, astr, nil];
  41. //Save
  42. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  43. NSString *documentsDirectory = [paths objectAtIndex:0];
  44. if (!documentsDirectory) {
  45.     NSLog(@"Documents directory not found!");
  46. }
  47. NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"Savedatas.plist"];
  48. [[NSArray arrayWithObjects:Array,nil] writeToFile:appFile atomically:NO];    
  49. //load
  50. if([[NSFileManager defaultManager] fileExistsAtPath:appFile])
  51.     self.SaveDataArray = [NSMutableArray arrayWithContentsOfFile:appFile];        
  52. else
  53.     self.SaveDataArray = [NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Savedatas" ofType:@"plist"]];
  54. NSArray *strArray = [self.SaveDataArray objectAtIndex:0];
  55. str = [strArray objectAtIndex:0];
  56. astr = [strArray objectAtIndex:1];
  57. //坛子里的,搬过来。。。。。
  58. -(BOOL) writeApplicationData:(NSDictionary *)data  writeFileName:(NSString *)fileName
  59. {
  60.     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  61.     NSString *documentsDirectory = [paths objectAtIndex:0];
  62.     if (!documentsDirectory) {
  63.         NSLog(@"Documents directory not found!");
  64.         return NO;
  65.     }
  66.     NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
  67.     return ([data writeToFile:appFile atomically:YES]);
  68. }
  69. -(id) readApplicationData:(NSString *)fileName
  70. {
  71.     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  72.     NSString *documentsDirectory = [paths objectAtIndex:0];
  73.     NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
  74.     NSDictionary *myData = [[[NSDictionary alloc] initWithContentsOfFile:appFile] autorelease];
  75.     return myData;
  76. }

原文链接:http://www.cnblogs.com/chen1987lei/archive/2011/04/14/2015971.html
<无标签>
举报
长平狐
发帖于6年前 0回/45阅
顶部