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
Posted in: iPhone SDK,Objective-C,Sample Code
Kirimkan Ini lewat Email
BlogThis!
Berbagi ke Twitter
Berbagi ke Facebook
0 komentar:
Posting Komentar