这是我的代码:
-(IBAction)btnSaveInfo:(UIButton *)sender {
NSMutableArray *data = [[NSMutableArray alloc] init];
NSDictionary *appInfo = [NSDictionary dictionaryWithObjectsAndKeys: fieldAPI.text, @"App API", fieldID.text, @"App ID", fieldName.text, @"App Name", nil];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *newPath = [documentsDirectory stringByAppendingPathComponent:@"AppData.plist"];
NSMutableArray *appdata = [NSMutableArray arrayWithContentsOfFile:newPath];
[appdata addObject:appInfo];
NSLog(@"%@",appdata);
[appdata writeToFile:newPath atomically:YES];
if ([data writeToFile:newPath atomically:YES]) {
NSLog(@"Success!");
NSMutableArray *finalData = [NSMutableArray arrayWithContentsOfFile:newPath];
NSLog(@"Final array:\n\n%@",finalData);
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}
else {
NSLog(@"Error...");
}
[data release];
}
我的NSLogs返回
2012-12-23 01:20:37.628 Parse Push[38052:c07] (
{
"App API" = asdf;
"App ID" = adsf;
"App Name" = asdf;
}
)
2012-12-23 01:20:37.932 Parse Push[38052:c07] Success!
2012-12-23 01:20:37.933 Parse Push[38052:c07] Final array:
(
)
我的代码是否有错误,或者我遗漏的内容?我是新手与.plists的互动,所以我很感激任何帮助,谢谢!
答案 0 :(得分:2)
问题出在这里
if ([data writeToFile:newPath atomically:YES]) {
你在这里再次写一个空数组
使用此
[appdata addObject:appInfo];
NSLog(@"%@",appdata);
if ([appdata writeToFile:newPath atomically:YES]){