我最近在使用plist存数数据,方便后面的数据浏览
很是苦恼于是自己就查遍各方资料,弄了一个简单的类函数,方便自己的更新数据
detailViewController.h1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| #import <Foundation/Foundation.h> #import <UIKit/UIKit.h>
@interface personIndex : NSObject
@property (strong, nonatomic) NSArray *personArr; @property (strong, nonatomic) NSMutableArray *personMulArr;
-(void) deletePlist;
-(void) showAlert;
-(void) getRemoteData;
-(void) createPlist;
-(void) writePlist;
-(NSArray *) readJsonData:(NSMutableData *)data;
-(void) executeDown;
@end
|
detailViewController.m1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
| #import "personIndex.h"
@implementation personIndex
@synthesize personArr; @synthesize personMulArr;
-(void) executeDown{ [self deletePlist]; [self getRemoteData]; [self createPlist]; [self writePlist]; }
-(void) writePlist{ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *path = [documentsDirectory stringByAppendingPathComponent:@"test.plist"]; NSFileManager *fileManager = [NSFileManager defaultManager]; if ([fileManager fileExistsAtPath: path]){ NSMutableDictionary *dictplist = [[NSMutableDictionary alloc ] init]; for(id item in self.personArr){ [dictplist setValue:[NSString stringWithFormat:@"%@",[item valueForKey:@"zh_name"]] forKey:[NSString stringWithFormat:@"%@",[item valueForKey:@"id"]]]; } if(![dictplist writeToFile:path atomically:YES]){ [self showAlert:@"同步数据失败"]; } }else{ NSString *appFile = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat: @"test.plist"] ]; NSMutableDictionary *dictplist = [[NSMutableDictionary alloc ] init]; for(id item in self.personArr){ [dictplist setValue:[NSString stringWithFormat:@"%@",[item valueForKey:@"zh_name"]] forKey:[NSString stringWithFormat:@"%@",[item valueForKey:@"id"]]]; }
if(![dictplist writeToFile:appFile atomically:YES]){ [self showAlert:@"同步数据失败"]; } } }
-(void) deletePlist{ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *path = [documentsDirectory stringByAppendingPathComponent:@"test.plist"]; NSFileManager *fileManager = [NSFileManager defaultManager]; [fileManager removeItemAtPath:path error:nil]; }
-(NSMutableDictionary *) readPlist{ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *path = [documentsDirectory stringByAppendingPathComponent:@"test.plist"]; NSFileManager *fileManager = [NSFileManager defaultManager]; NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: path]; if ([fileManager fileExistsAtPath: path]){ data = [[NSMutableDictionary alloc] initWithContentsOfFile: path]; }else{ data = [[NSMutableDictionary alloc] init]; } return data; }
-(void) createPlist{ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *path = [documentsDirectory stringByAppendingPathComponent:@"test.plist"]; NSFileManager *fileManager = [NSFileManager defaultManager]; if (![fileManager fileExistsAtPath: path]){ [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat: @"test.plist"]]; } }
-(void) getRemoteData{ 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"]; NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60]; NSURLResponse *response = [[NSURLResponse alloc] init]; NSError *error = [[NSError alloc] init]; NSMutableData *receivedData = (NSMutableData *)[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; self.personArr = [self readJsonData:receivedData]; }
-(NSArray *) readJsonData:(NSMutableData *)data{ NSArray *personList = [[NSArray alloc] init]; if(data == nil){ [self showAlert:@"更新数据失败"]; }else{ NSError *error; NSDictionary *personDictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error]; NSDictionary *personInfo = [personDictionary objectForKey:@"data"]; personList = [personInfo objectForKey:@"list"]; } return personList; }
-(NSArray *) readStringJsonData:(NSMutableData *)data{ NSError *error; NSDictionary *personDictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error]; NSDictionary *personInfo = [personDictionary objectForKey:@"data"]; NSArray *personList = [personInfo objectForKey:@"list"]; return personList; }
-(void) showAlert:(NSString *)stringData{ UIAlertView *alrt = [[UIAlertView alloc] initWithTitle:@"同步数据失败" message:[NSString stringWithFormat:@"%@",stringData] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alrt show]; }
@end
|