objective-c - NSFileHandle readInBackgroundAndNotify 不起作用

标签 objective-c cocoa nsnotifications

您好,我正在使用 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");
}

但是,选择器永远不会被调用,也不会收到通知。

最佳答案

  1. 将 NSLog 添加到 startReading 以确保调用它。
  2. 记录fh。我的猜测是它是nil(很可能是因为您还没有创建 MyTestApp.log)。

关于objective-c - NSFileHandle readInBackgroundAndNotify 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1324152/

相关文章:

objective-c - 处理 View Controller 或 subview 中的通知

ios - NSNotification 和控制流

objective-c - 如何从c函数获取数据到cocoa(objective c)函数

iphone - 调用协议(protocol)方法是否通过程序流控制?

iOS 调整 UITableheaderView 的大小

Swift MacOS SpriteKit rightMouseDragged 不起作用

ios - 比较包含 NSStrings 的 NSDictionaries

ios - 在 Objective C 中调用父类(super class)方法的时机是否重要?

ios - 无法将类型 '[UICollectionViewLayoutAttributes]?' 的值转换为指定类型 'NSMutableArray'

ios - 有没有办法检查观察者是否在听一些 NSNotification?