質問

Here is my code:

-(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];
    }

My NSLogs return

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:

(
)

Is there an error with my code, or something that I'm missing? I am new to interacting with .plists, so I would appreciate any help, thanks!

役に立ちましたか?

解決

The problem is here

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

You are writing again an empty array here

Use this

[appdata addObject:appInfo];
NSLog(@"%@",appdata);
if ([appdata writeToFile:newPath atomically:YES]){
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top