Logging by Appending to a File

suggest change

NSLog writes to the console. You can also append to a file. This assumes self is an NSString, and that path and enc (NSStringEncoding) are already defined:

NSFileHandle* fh = [NSFileHandle fileHandleForWritingAtPath:path];
if ( !fh ) {
    [[NSFileManager defaultManager] createFileAtPath:path contents:nil attributes:nil];
    fh = [NSFileHandle fileHandleForWritingAtPath:path];
}
if ( fh ) {
    @try {
        [fh seekToEndOfFile];
        [fh writeData:[self dataUsingEncoding:enc]];
    }
    @catch (NSException *exception) {
    }
    [fh closeFile];
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents