【问题标题】:Writing to .plist returns "Success!" but ends up blank写入 .plist 会返回“成功!”但最终空白
【发布时间】:2012-12-23 06:33:55
【问题描述】:

这是我的代码:

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

【问题讨论】:

  • NSLog(@"%@",appdata); 呢?
  • 刚刚编辑它以包含应用程序数据...@InderKumarRathore

标签: objective-c plist writetofile


【解决方案1】:

问题来了

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

这里又写了一个空数组

使用这个

[appdata addObject:appInfo];
NSLog(@"%@",appdata);
if ([appdata writeToFile:newPath atomically:YES]){

【讨论】:

  • 哇!!我不敢相信我错过了:) 谢谢(我会尽快接受你的回答!)
猜你喜欢
  • 2011-05-30
  • 1970-01-01
  • 1970-01-01
  • 2010-09-07
  • 2019-05-05
  • 1970-01-01
  • 2014-02-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多