1
我有我的应用程序的方法是将一个字符串写入到文件的末尾:NSFileHandle fileHandleForWriting返回nil,错误代码24
-(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];
}
它工作了一段时间,然后开始出现问题; fileHandleForWritingToURL
返回nil
。我得到的错误是NSCocoaErrorDomain错误24.但是我无法在任何地方找到对此错误的任何引用。谷歌没有帮助。有没有人见过这个?难道我做错了什么?
是的,我添加了零检查,因为它只是停止写作,我不知道为什么。如果它有“打开的文件太多”,我没有正确关闭文件? –
您肯定需要调用closeFile函数,否则fileHandleForWritingToURL将最终失败,并出现NSCocoaErrorDomain错误24.您是否使用ARC? self.logPath是否更改? self.logPath的价值是什么?在它失败之前,你可以调用你的日志方法多少次? – mjswan
'self.logPath'是我要写入的字符串路径。我通过抓取文档目录来获得它。你可以把它看作是一个在函数中定义的静态字符串,它是一个有效的路径。它不会改变。是的,这是使用ARC。 –