保存到plist文件问题

时间:2013-02-27 15:49:42

标签: objective-c nsmutablearray plist nsmutabledictionary

出于某种原因,我无法将新数据保存到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];
     }

这是我用来将文件从bundle复制到app目录的代码,如果它不存在的话:

    -(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

0 个答案:

没有答案