NSFileHandle fileHandleForWriting returns nil, error code 24

1k views Asked by At

I've got a method in my app that writes a string to the end of a file:

-(void)log:(NSString *)str
{
  if (![[NSFileManager defaultManager] fileExistsAtPath:self.logPath])
    [[NSFileManager defaultManager] createFileAtPath:self.logPath contents:nil attributes:nil];

  NSError *err = nil;
NSFileHandle *myHandle = [NSFileHandle fileHandleForWritingToURL:[NSURL fileURLWithPath:self.logPath] error:&err];
if (!myHandle)
    NSLog(@"Failed to write file - %@", err.localizedDescription);
  [myHandle seekToEndOfFile];
  [myHandle writeData:[str dataUsingEncoding:NSUTF8StringEncoding]];
  [myHandle closeFile];
}

It works for a while and then starts failing; fileHandleForWritingToURL returns nil. The error I get is NSCocoaErrorDomain error 24. But I can't find any reference to this error anywhere. Google is no help. Has anyone seen this before? Am I doing something wrong?

1

There are 1 answers

6
mjswan On BEST ANSWER

My sense is that NSCocoaErrorDomain is mapped to UNIX errno values, and errno 24 is "too many open files". Have a close look at the NSFileHandle class reference. Also,

[myHandle seekToEndOfFile]; [myHandle writeData:[str dataUsingEncoding:NSUTF8StringEncoding]]; [myHandle closeFile];

should be in the else case of your

if (!myHandle)

test.