我有一个应用程序可以自行从 url 获取 json 并将值存储在 .plist 文件中。
这是我的代码,但不工作,我的 plist 文件为空(请指导我)
这是我的 json :
[
{
"id": "1",
"title": "Apple Releases iPad 3",
"article": "This week Apple Inc. released the third gen iPad. It's available now for $599. It comes in 2 models: 8GB and 16GB.",
"timestamp": "1343782587",
"date_string": "Tue, Jul 31st, 2012 @ 7:56"
},
{
"id": "2",
"title": "Microsoft Releases Windows 8",
"article": "Last week Microsoft released Windows 8 for consumer purchase. It's available now starting at $99 for the Home version. It comes with Microsoft Office '12 already installed.",
"timestamp": "1343782649",
"date_string": "Tue, Jul 31st, 2012 @ 7:57"
},
{
"id": "3",
"title": "Blizzard Announces Diablo IV",
"article": "This week, long-time Diablo fans are euphoric with excitement at the announcement of Diablo IV, which will be released this fall, for $20! This original $20 purchase will also include all future expansion packs.",
"timestamp": "1343782742",
"date_string": "Tue, Jul 31st, 2012 @ 7:59"
}
]
-(NSString*)docsDir {
return [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory,
NSUserDomainMask, YES)objectAtIndex:0];
}
- (void)viewDidLoad {
[super viewDidLoad];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSURL *url = [NSURL URLWithString:@"http://192.168.1.109/mamal/json.php"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLConnection *con = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[con start];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
data = [[NSMutableData alloc]init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData {
[data appendData:theData];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
name = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
for (int i =0; i<[name count]; i++) {
NSIndexPath *indexPath = [self.table indexPathForSelectedRow];
n = [[name objectAtIndex:(indexPath.row)+i]objectForKey:@"title"];
if(!add){
add = [NSMutableArray array];
}
[add addObject:n];
}
NSLog(@"add : %@",add);
listPath = [[self docsDir]stringByAppendingFormat:@"janatan.plist"];
array = [NSMutableArray arrayWithContentsOfFile:listPath];
[array addObjectsFromArray:add];
[array writeToFile:listPath atomically:YES];
NSLog(@"array : %@",array); //array is null :(
[table reloadData];
}