0

这是我的代码:

-(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 交互的新手,因此我将不胜感激,谢谢!

4

1 回答 1

2

问题就在这里

if ([data writeToFile:newPath atomically:YES]) {

你在这里又写了一个空数组

用这个

[appdata addObject:appInfo];
NSLog(@"%@",appdata);
if ([appdata writeToFile:newPath atomically:YES]){
于 2012-12-23T06:39:10.200 回答