我有一个应用程序在自我中从url中获取json并将值存储在.plist文件中。当从json获取数据并在plist中存储时,为plist获得null值
这是我的代码,但没有工作,在空我的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];
}
listPath = [[self docsDir] stringByAppendingFormat:@“janatan.plist”]; - >是这是什么NULL – 2013-04-23 19:23:05
没有我的朋友我有3个值在“添加”但“阵列”为空。 [数组addObjectFromArray:add];看这个代码! – janatan 2013-04-23 19:29:48