您好,我正在使用 NSFileHandle 的 readInBackgroundAndNotify 方法在日志文件更新时获取通知。
我有以下代码:
- (void)startReading
{
NSString *logPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Logs/MyTestApp.log"];
NSFileHandle *fh = [NSFileHandle fileHandleForReadingAtPath:logPath];
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self
selector:@selector(getData:)
name:NSFileHandleReadCompletionNotification
object:fh];
[fh readInBackgroundAndNotify];
}
- (void) getData: (NSNotification *)aNotification
{
NSLog(@"notification received");
}
但是,选择器永远不会被调用,也不会收到通知。
最佳答案
- 将 NSLog 添加到
startReading
以确保调用它。 - 记录
fh
。我的猜测是它是nil
(很可能是因为您还没有创建 MyTestApp.log)。
关于objective-c - NSFileHandle readInBackgroundAndNotify 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1324152/