Without further ado:
#import <Foundation/Foundation.h>
@interface NSData(MCFileAppend)
- (BOOL)appendToFile:(NSString *)path;
@end
@implementation NSData(MCFileAppend)
- (BOOL)appendToFile:(NSString *)path
{
NSFileHandle *fh = [NSFileHandle fileHandleForWritingAtPath:path];
if (fh == nil)
return [self writeToFile:path atomically:YES];
[fh truncateFileAtOffset:[fh seekToEndOfFile]];
[fh writeData:self];
[fh closeFile];
return YES;
}
@end
#pragma mark -
@interface NSString(MCFileAppend)
- (BOOL)appendToFile:(NSString *)path usingEncoding:(NSStringEncoding)encoding;
@end
@implementation NSString(MCFileAppend)
- (BOOL)appendToFile:(NSString *)path usingEncoding:(NSStringEncoding)encoding
{
NSData *encoded = [self dataUsingEncoding:encoding];
return [encoded appendToFile:path];
}
@end
No comments:
Post a Comment