IOS plist文件的增删操作

来源:互联网 发布:ubuntu关闭网卡 编辑:程序博客网 时间:2024/05/20 10:56


我最近在使用plist存数数据,方便后面的数据浏览

很是苦恼于是自己就查遍各方资料,弄了一个简单的类函数,方便自己的更新数据

detailViewController.h

  1. #import <Foundation/Foundation.h>
  2. #import <UIKit/UIKit.h>
  3.  
  4. @interface personIndex : NSObject
  5.  
  6. @property (strong, nonatomic) NSArray *personArr;
  7. @property (strong, nonatomic) NSMutableArray *personMulArr;
  8.  
  9. //清空原有数据
  10. -(void) deletePlist;
  11. //响应错误
  12. -(void) showAlert;
  13. //获取远端数据
  14. -(void) getRemoteData;
  15. //创建plist文件
  16. -(void) createPlist;
  17. //写入数据到plist文件
  18. -(void) writePlist;
  19. //解析json数据
  20. -(NSArray *) readJsonData:(NSMutableData *)data;
  21. //下载数据
  22. -(void) executeDown;
  23.  
  24. @end
detailViewController.m

  1. #import "personIndex.h"
  2.  
  3. @implementation personIndex
  4.  
  5. @synthesize personArr;
  6. @synthesize personMulArr;
  7.  
  8.  
  9. -(void) executeDown{
  10. [self deletePlist];
  11. [self getRemoteData];
  12. [self createPlist];
  13. [self writePlist];
  14. }
  15.  
  16. //将数据写入plist
  17. -(void) writePlist{
  18. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  19. NSString *documentsDirectory = [paths objectAtIndex:0];
  20. NSString *path = [documentsDirectory stringByAppendingPathComponent:@"test.plist"];
  21. NSFileManager *fileManager = [NSFileManager defaultManager];
  22. if ([fileManager fileExistsAtPath: path]){//如果文件存在,写入数据
  23. //创建字典
  24. NSMutableDictionary *dictplist = [[NSMutableDictionary alloc ] init];
  25. for(id item in self.personArr){
  26. //设置属性值
  27. [dictplist setValue:[NSString stringWithFormat:@"%@",[item valueForKey:@"zh_name"]]
  28. forKey:[NSString stringWithFormat:@"%@",[item valueForKey:@"id"]]];
  29. }
  30. //写入文件
  31. if(![dictplist writeToFile:path atomically:YES]){
  32. [self showAlert:@"同步数据失败"];
  33. }
  34. }else{
  35. NSString *appFile = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat: @"test.plist"] ];
  36. //创建字典
  37. NSMutableDictionary *dictplist = [[NSMutableDictionary alloc ] init];
  38. for(id item in self.personArr){
  39. //设置属性值
  40. [dictplist setValue:[NSString stringWithFormat:@"%@",[item valueForKey:@"zh_name"]]
  41. forKey:[NSString stringWithFormat:@"%@",[item valueForKey:@"id"]]];
  42. }
  43.  
  44. ///写入文件
  45. if(![dictplist writeToFile:appFile atomically:YES]){
  46. [self showAlert:@"同步数据失败"];
  47. }
  48. }
  49. }
  50.  
  51.  
  52. -(void) deletePlist{
  53. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  54. NSString *documentsDirectory = [paths objectAtIndex:0];
  55. NSString *path = [documentsDirectory stringByAppendingPathComponent:@"test.plist"];
  56. NSFileManager *fileManager = [NSFileManager defaultManager];
  57. [fileManager removeItemAtPath:path error:nil];
  58. }
  59.  
  60.  
  61. -(NSMutableDictionary *) readPlist{
  62. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  63. NSString *documentsDirectory = [paths objectAtIndex:0];
  64. NSString *path = [documentsDirectory stringByAppendingPathComponent:@"test.plist"];
  65. NSFileManager *fileManager = [NSFileManager defaultManager];
  66. NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
  67. if ([fileManager fileExistsAtPath: path]){//如果文件存在,写入数据
  68. data = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
  69. }else{
  70. // If the file doesn’t exist, create an empty dictionary
  71. data = [[NSMutableDictionary alloc] init];
  72. }
  73. return data;
  74. }
  75.  
  76. //创建plist文件
  77. -(void) createPlist{
  78. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  79. NSString *documentsDirectory = [paths objectAtIndex:0];
  80. NSString *path = [documentsDirectory stringByAppendingPathComponent:@"test.plist"];
  81. NSFileManager *fileManager = [NSFileManager defaultManager];
  82. if (![fileManager fileExistsAtPath: path]){
  83. [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat: @"test.plist"]];
  84. }
  85. }
  86.  
  87. -(void) getRemoteData{
  88. NSURL *url = [[NSURL alloc] initWithString:@"http://222.73.93.70:3002/person/actorlist?order_by=index&start_date=2012-06-01&end_date=2012-07-01&start=1&offset=10&app_key=KSKdzSyeb99YdLwTMrzvuLumNYCM6pzT4Z3f27R4L3qq6jCs"];
  89. NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url
  90. cachePolicy:NSURLRequestUseProtocolCachePolicy
  91. timeoutInterval:60];
  92. NSURLResponse *response = [[NSURLResponse alloc] init];
  93. NSError *error = [[NSError alloc] init];
  94. NSMutableData *receivedData = (NSMutableData *)[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
  95. self.personArr = [self readJsonData:receivedData];
  96. }
  97.  
  98. -(NSArray *) readJsonData:(NSMutableData *)data{
  99. //NSJSONSerialization提供了将JSON数据转换为Foundation对象(一般都是NSDictionary和NSArray)
  100. //和Foundation对象转换为JSON数据(可以通过调用isValidJSONObject来判断Foundation对象是否可以转换为JSON数据)。
  101. NSArray *personList = [[NSArray alloc] init];
  102. if(data == nil){
  103. [self showAlert:@"更新数据失败"];
  104. }else{
  105. NSError *error;
  106. NSDictionary *personDictionary = [NSJSONSerialization JSONObjectWithData:data
  107. options:NSJSONReadingMutableContainers
  108. error:&error];
  109. NSDictionary *personInfo = [personDictionary objectForKey:@"data"];
  110. personList = [personInfo objectForKey:@"list"];
  111. }
  112. return personList;
  113. }
  114.  
  115. -(NSArray *) readStringJsonData:(NSMutableData *)data{
  116. //NSJSONSerialization提供了将JSON数据转换为Foundation对象(一般都是NSDictionary和NSArray)
  117. //和Foundation对象转换为JSON数据(可以通过调用isValidJSONObject来判断Foundation对象是否可以转换为JSON数据)。
  118. NSError *error;
  119. NSDictionary *personDictionary = [NSJSONSerialization JSONObjectWithData:data
  120. options:NSJSONReadingMutableContainers
  121. error:&error];
  122. NSDictionary *personInfo = [personDictionary objectForKey:@"data"];
  123. NSArray *personList = [personInfo objectForKey:@"list"];
  124. return personList;
  125. }
  126.  
  127. -(void) showAlert:(NSString *)stringData{
  128. UIAlertView *alrt = [[UIAlertView alloc] initWithTitle:@"同步数据失败"
  129. message:[NSString stringWithFormat:@"%@",stringData]
  130. delegate:self
  131. cancelButtonTitle:@"OK"
  132. otherButtonTitles:nil];
  133. [alrt show];
  134. }
  135.  
  136. @end
原创粉丝点击