クラスを初期化するとき:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *rootPath = [paths objectAtIndex:0];
NSString *logFilePath = [path stringByAppendingPathComponent:@"log.txt"];
if(![fm fileExistsAtPath:logFilePath]) {
unsigned char data = {' '};
[fm createFileAtPath:logFilePath contents:[NSData dataWithBytes:&data length:1] attributes:nil];
}
NSFileHandle *logFile = [NSFileHandle fileHandleForUpdatingAtPath:logFilePath];
次に、logFile へのハンドルを保持し、いつログに記録するかを確認します。count
および「oldCount」変数に現在および以前のカウントがあると仮定します。
NSString *message = [NSString stringWithFormat:@"Number at start: %i - button pressed: [%@], new number: %i\n", oldCount, count > oldCount ? @"up" : @"down", count];
NSData *dataLog = [message dataUsingEncoding: NSUTF8StringEncoding];
[logFile seekToEndOfFile];
[logFile writeData:dataLog];