0

I have a method that uses ARC and takes an NSError pointer and I pass that into the contentsOfDirectoryAtPath:error: method of NSFileManager like so:

+ (NSArray *) getContentsOfCurrentDirectoryWithError: (NSError **)error
{
    // code here

    _contentsOfCurrentDirectory = [_fileManager contentsOfDirectoryAtPath: _documentDirectory error: error];

    // more code here
}

I'm not sure if this code is correct, because I'm not very used to pointers due to being spoiled by managed languages. However, my initial reaction was to do this:

_contentsOfCurrentDirectory = [_fileManager contentsOfDirectoryAtPath: _documentDirectory error: &error];

Xcode yelled at me for trying it like that, though. My assumption as to how this might work is that the contentsOfDirectoryAtPath:error: method is asking for a pointer and, since I was asking for a pointer in my getContentsOfCurrentDirectoryWithError:error: method, I can just pass error without using the dereference operator.

I just want to make sure I'm doing this right to avoid hassle later, so is there a problem with what I have?

Thick_propheT
  • 1,003
  • 2
  • 10
  • 29

1 Answers1

0

Method can be like this NSError **errorMessage in .h file:

+ (NSArray *) getContentsOfCurrentDirectory
{ 
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,    NSUserDomainMask, YES);
  NSString *_documentsDirectory = [paths objectAtIndex:0]; 
  NSArray *_contentsOfCurrentDirectory = [[NSFileManager defaultManger]  contentsOfDirectoryAtPath: _documentDirectory error: errorMessage]; 
  return contentsOfCurrentDirectory ;
}

Whenever error occurs errorMessage reference will have error message [error description]

Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
  • Well, I had intended for my custom method to be able to expose any errors that may have been encountered by the inner method. – Thick_propheT Jun 13 '12 at 15:55