1

I have in my code an array of custom objects.

i want to write this array in a file on the documents folder. in this answer iPhone - archiving array of custom objects i saw that i need to implement this methods:

- (void)encodeWithCoder:(NSCoder *)aCoder;
- (id)initWithCoder:(NSCoder *)aDecoder;

so i implemented them:

- (void)encodeWithCoder:(NSCoder *)encoder {
  [encoder encodeObject:self.data forKey:@"data"];
  [encoder encodeObject:self.nome forKey:@"nome"];
  [encoder encodeObject:self.celular forKey:@"celular"];
  [encoder encodeObject:self.endereco forKey:@"endereco"];
  [encoder encodeObject:self.horaConclusao forKey:@"horaConclusao"];
  [encoder encodeObject:self.horaAtendimento forKey:@"horaAtendimento"];
}

- (id)initWithCoder:(NSCoder *)decoder {
  self = [super init];
  if (self) {
    self.data = [decoder decodeObjectForKey:@"data"];
    self.nome = [decoder decodeObjectForKey:@"nome"];
    self.celular = [decoder decodeObjectForKey:@"celular"];
    self.endereco = [decoder decodeObjectForKey:@"endereco"];
    self.horaConclusao = [decoder decodeObjectForKey:@"horaConclusao"];
    self.horaAtendimento = [decoder decodeObjectForKey:@"horaAtendimento"];
  }

  return self;
}

and in my code i write using this method:

this code is to remove old files

-(NSString *) plistHistoryFile {
  NSError *error;
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentsDirectory = [paths objectAtIndex:0];
  NSString *path = [documentsDirectory stringByAppendingPathComponent:[nameFile stringByAppendingPathExtension:@"plist"]];

  NSFileManager *filemgr = [NSFileManager defaultManager];
  if ([filemgr fileExistsAtPath:path]) {
    [filemgr removeItemAtPath:path error:&error];
  }

  return path;
}

and i call the write in this method:

-(void) writeArrayToHistoryFile:(NSArray *) array {
  NSString *path = [self plistHistoryFile];
  NSLog(@"%@", path);

  if ([array writeToFile:path atomically:NO]) {
    NSLog(@"YES");
  } else {
    NSLog(@"NO");
  }
}

but my response on the log is always NO, what am i doing wrong?

1 Answer 1

0

You need to find out what the error is, but you can't get the error using -[NSArray writeToFile:atomically:]. Instead, write the file this way:

NSError *error;

NSData *data = [NSPropertyListSerialization dataWithPropertyList:array
    format: NSPropertyListBinaryFormat_v1_0 options:0 error:&error];
if (!data) {
    NSLog(@"failed to convert array to data: %@", error);
    return;
}

if (![data writeToFile:path options:NSDataWritingAtomic error:&error]) {
    NSLog(@"failed to write data to file: %@", error);
    return;
}

NSLog(@"wrote data successfully");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.