Skip to Content

NSMutableArray __--__ writeToFile / arrayWithContentsOfFile Example

Posted on
  NSMutableArray *a = [[NSMutableArray alloc] init];

  [a addObject:@"123166"];
  [a addObject:@"122377"];
  [a addObject:@"123188"];
  [a addObject:@"123199"];
  [a addObject:@"123100"];

  for (NSString *ss in a)
  {
    NSLog(@"%@", ss);
  }

  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentsDirectory = [paths objectAtIndex:0];
  NSString *fileName = [documentsDirectory stringByAppendingPathComponent:@"example.dat"];

  [a writeToFile:fileName atomically:YES];

  //

  NSMutableArray *aa = [NSMutableArray arrayWithContentsOfFile:fileName];
  NSLog(@"array count %d", [aa count]);

  for (NSString *ss in aa)
  {
    NSLog(@"result:%@", ss);
  }