iOS: store two NSMutableArray in a .plist file

后端 未结 1 449
温柔的废话
温柔的废话 2020-11-30 13:08

I want to store two NSMutableArray that I use as global array in AppDelegate. These two array are also store with NSUserDefaults. Now I want to know how I must create this f

1条回答
  •  春和景丽
    2020-11-30 13:16

    1. Create an NSArray containing your two NSMutableArrays.

      NSArray *array = [NSArray arrayWithObjects:<#(id), ...#>, nil];
      
    2. Write the array to a file.

      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
      NSString *libraryDirectory = [paths objectAtIndex:0];
      NSString *location = [libraryDirectory stringByAppendingString:@"/somefilename.plist"];
      [array writeToFile:location atomically:YES];
      
    3. Load the array from the file.

      NSString *path = [bundle pathForResource:@"file" ofType:@"plist"];
      NSArry *array = (path != nil ? [NSArray arrayWithContentsOfFile:location] : nil);
      

    0 讨论(0)
提交回复
热议问题