zoukankan      html  css  js  c++  java
  • 保存数据的一系列方法

    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. }
  • 相关阅读:
    知识付费时代:屌丝程序员如何用技术实现
    过完年了,要不要辞职?
    程序猿不得不知道的业内“黑话”
    Go 2 Draft Designs
    11 Go 1.11 Release Notes
    10 Go 1.10 Release Notes
    09 Go 1.9 Release Notes
    win10系统电脑无法识别u盘的解决办法
    IDEA导入Git项目后右键项目找不到Git选项的解决方法
    Redis在windows下安装与配置
  • 原文地址:https://www.cnblogs.com/chen1987lei/p/2015971.html
Copyright © 2011-2022 走看看