0

由于某种原因,我无法将新数据保存到 plist 文件中。这是我一直用来保存数据的代码:

   -(void)saveData:(NSMutableDictionary *)dictionaryData toFile:(NSString *)filename {
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     NSString *docDir = [paths objectAtIndex:0];
     NSString *path = [docDir stringByAppendingPathComponent:filename];
     NSMutableArray *data = [[NSMutableArray alloc] initWithContentsOfFile:path];
    [data addObject:dictionaryData];
    [data writeToFile:filename atomically:YES];
     }

这是我用来将文件从包复制到应用程序目录的代码,以防万一它不存在:

    -(NSMutableArray *)loadFromFile:(NSString *)filename {
     NSError *error;
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     NSString *docDir = [paths objectAtIndex:0];
     NSString *path = [docDir stringByAppendingPathComponent:filename];
     NSFileManager *fileMgr = [NSFileManager defaultManager];
     if(![fileMgr fileExistsAtPath:path]) {
        NSArray *fileArray = [filename componentsSeparatedByString:@"."];
        NSString *name = [fileArray objectAtIndex:0];
        NSString *ext = [fileArray objectAtIndex:1];
        NSString *bundle = [[NSBundle mainBundle] pathForResource:name ofType:ext];
        [fileMgr copyItemAtPath:bundle toPath:path error:&error];
     }
     NSMutableArray *data = [[NSMutableArray alloc] initWithContentsOfFile:path];
     return data;
     }

由于某种原因,我无法将新数据保存到 plist 文件中。当我尝试将新的 NSMutableDictionary 对象添加到我的 plist 文件(使用 saveData:toFile: 方法)并且使用 plist 文件数据重新加载我的数组变量时 - 新对象不存在。难道我做错了什么?

这就是我加载 plist 文件的方式:

 @property (nonatomic, strong) NSMutableArray *modules;
 @property (nonatomic, strong) NSMutableDictionary *module; 

  - (void)viewDidLoad {
    [super viewDidLoad];

    self.modules = [self loadFromFile:@"ModulesList.plist"];
    self.module = [self.modules objectAtIndex:0];

    for (int i = 0; i < self.modules.count; i++ ) {
        NSLog(@"Modules array from plist file, module at index %i : %@",i, [self.modules         objectAtIndex:i]);
    }

而不是为了测试目的,我有这个代码来添加新的模块对象:

    - (IBAction)leftButton:(id)sender {
      NSString *mytitle = [[NSString alloc] initWithFormat:@"my title"];
      NSString *myauthor = [[NSString alloc] initWithFormat:@"my author2"];
      NSUInteger myean = 22023423;
      NSString *mytitle2 = [[NSString alloc] initWithFormat:@"my title 2"];
      NSString *myauthor2 = [[NSString alloc] initWithFormat:@"my author2"];
      NSUInteger myean2 = 29032432;
      NSString *mytitle3 = [[NSString alloc] initWithFormat:@"my title 3"];
      NSString *myauthor3 = [[NSString alloc] initWithFormat:@"my author 3"];
      NSUInteger myean3 = 21023423;


      NSMutableDictionary *mybook = [[NSMutableDictionary alloc] init];
      NSMutableDictionary *mybook2 = [[NSMutableDictionary alloc] init];
      NSMutableDictionary *mybook3 = [[NSMutableDictionary alloc] init];

      [mybook setObject:mytitle forKey:@"title"];
      [mybook setObject:myauthor forKey:@"author"];
      [mybook setObject:[NSNumber numberWithInteger:myean] forKey:@"ean"];

      [mybook2 setObject:mytitle2 forKey:@"title"];
      [mybook2 setObject:myauthor2 forKey:@"author"];
      [mybook2 setObject:[NSNumber numberWithInteger:myean2] forKey:@"ean"];

      [mybook3 setObject:mytitle3 forKey:@"title"];
      [mybook3 setObject:myauthor3 forKey:@"author"];
      [mybook3 setObject:[NSNumber numberWithInteger:myean3] forKey:@"ean"];

      NSMutableArray *mybooks = [[NSMutableArray alloc] init];

      [mybooks addObject:mybook];
      [mybooks addObject:mybook2];
      [mybooks addObject:mybook3];

      [self.module setObject:mybooks forKey:@"books"];
      [self.modules addObject:self.module];


      for (int i = 0; i < self.modules.count; i++ ) {
          NSLog(@"Modules array after add operation, module at index: %i: %@",i, [self.modules objectAtIndex:i]);
}

      [self saveData:self.module toFile:@"ModulesList.plist"];   
   }

当我使用按钮操作从 plist 重新加载我的 self.modules 数组时,我的新数据不存在:

       - (IBAction)reload:(id)sender {
         self.modules = [self loadFromFile:@"ModulesList.plist"];
         for (int i = 0; i < self.modules.count; i++ ) {
             NSLog(@"RELOAD: Modules array from plist file, module at index %i : %@",i,[self.modules objectAtIndex:i]);
         }

   }

这是我的 plist 文件的屏幕截图:http: //dl.dropbox.com/u/49076351/Screen%20Shot%202013-02-27%20at%2016.27.45.png

4

0 回答 0