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

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

detailViewController.h
1
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;
//创建plist文件
-(void) createPlist;
//写入数据到plist文件
-(void) writePlist;
//解析json数据
-(NSArray *) readJsonData:(NSMutableData *)data;
//下载数据
-(void) executeDown;

@end
detailViewController.m
1
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];
}

//将数据写入plist
-(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{
// If the file doesn’t exist, create an empty dictionary
data = [[NSMutableDictionary alloc] init];
}
return data;
}

//创建plist文件
-(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{
//NSJSONSerialization提供了将JSON数据转换为Foundation对象(一般都是NSDictionary和NSArray)
//和Foundation对象转换为JSON数据(可以通过调用isValidJSONObject来判断Foundation对象是否可以转换为JSON数据)。
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{
//NSJSONSerialization提供了将JSON数据转换为Foundation对象(一般都是NSDictionary和NSArray)
//和Foundation对象转换为JSON数据(可以通过调用isValidJSONObject来判断Foundation对象是否可以转换为JSON数据)。
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