Objective c 使用FSEventStreamCreate创建的流为零

Objective c 使用FSEventStreamCreate创建的流为零,objective-c,macos,filesystems,macos-carbon,fsevents,Objective C,Macos,Filesystems,Macos Carbon,Fsevents,我想创建一个简单的事件流,以便在目录中发生某些更改时侦听事件。第一步是创建流,但我在使用FSEventStreamCreate函数创建流时收到一个错误。谷歌搜索这个错误是没有用的,我不明白错误在哪里 (CarbonCore.framework) FSEventStreamCreate: _FSEventStreamCreate: ERROR: (CFStringGetTypeID() != CFGetTypeID(cfStringRef)) (i = 0) 我的代码与 无论如何,这是我的代码:

我想创建一个简单的事件流,以便在目录中发生某些更改时侦听事件。第一步是创建流,但我在使用FSEventStreamCreate函数创建流时收到一个错误。谷歌搜索这个错误是没有用的,我不明白错误在哪里

(CarbonCore.framework) FSEventStreamCreate: _FSEventStreamCreate: ERROR: (CFStringGetTypeID() != CFGetTypeID(cfStringRef)) (i = 0)
我的代码与

无论如何,这是我的代码:

static void gotEvent(ConstFSEventStreamRef stream, 
                 void *clientCallBackInfo, 
                 size_t numEvents, 
                 void *eventPathsVoidPointer, 
                 const FSEventStreamEventFlags eventFlags[], 
                 const FSEventStreamEventId eventIds[]
                 ) {

    NSLog(@"File Changed!");
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    NSString *fileInputPath = @"/tmp/mamma/ciao.txt";
    FSEventStreamRef stream = [self eventStreamForFileAtPath: fileInputPath];
}

- (FSEventStreamRef) eventStreamForFileAtPath: (NSString *) fileInputPath
{
    if (![[NSFileManager defaultManager] fileExistsAtPath:fileInputPath]) {
        @throw [NSException exceptionWithName: @"FileNotFoundException"
                                   reason:@"There is not file at path specified in fileInputPath"
                                 userInfo: nil];
    }
    CFStringRef fileInputDir = (CFStringRef)[fileInputPath stringByDeletingLastPathComponent];
    CFArrayRef pathsToWatch = CFArrayCreate(NULL, (const void **) fileInputDir, 1, NULL);
    void *callbackInfo = NULL;
    CFAbsoluteTime latency = 3.0; /* Latency in seconds */

    FSEventStreamRef stream = FSEventStreamCreate(
                                                 NULL, 
                                                 &gotEvent, 
                                                 callbackInfo, 
                                                 pathsToWatch, 
                                                 kFSEventStreamEventIdSinceNow, 
                                                 latency, 
                                                 kFSEventStreamEventFlagNone
                                                 );

    return stream;
}
请尝试以下代码:

- (FSEventStreamRef) eventStreamForFileAtPath: (NSString *) fileInputPath  {

   if (![[NSFileManager defaultManager] fileExistsAtPath:fileInputPath]) {
       @throw [NSException exceptionWithName:@"FileNotFoundException"
                                      reason:@"There is not file at path specified in fileInputPath"
                                    userInfo:nil];
   }

   NSString *fileInputDir = [fileInputPath stringByDeletingLastPathComponent];

   NSArray *pathsToWatch = [NSArray arrayWithObjects:fileInputDir, nil];

   void *appPointer = (void *)self;

   FSEventStreamContext context = {0, appPointer, NULL, NULL, NULL};

   NSTimeInterval latency = 3.0;

   FSEventStreamRef stream = FSEventStreamCreate(NULL,
                                                  &gotEvent,                                 
                                                  &context,                                 
                                                  (CFArrayRef) pathsToWatch,                                 
                                                  kFSEventStreamEventIdSinceNow,                                 
                                                  (CFAbsoluteTime) latency,                                 
                                                  kFSEventStreamCreateFlagUseCFTypes                                 
                                                  );

   FSEventStreamScheduleWithRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
   FSEventStreamStart(stream);

   return stream;

}

另请查看此链接:。它向您展示了如何监控Cocoa应用程序中的文件更改。

通过您的代码,我终于收到了一个有效的流对象。我不明白为什么你的代码有效而我的代码无效!我看到的唯一区别是,您正在强制转换NSArray,而不是直接创建CFArrayRef。您也有不同的上下文,但它似乎对创建流没有影响您的方法将起作用,只是您忘记在
CFArrayCreate
函数中的
fileInputDir
之前放置一个引用运算符(&)。我也没有看到您调用
FSEventStreamScheduleWithRunLoop()
FSEventStreamStart()
。您还应该阅读我提供的链接,它将向您展示如何在完成后停止并使流无效。问题在于数组的创建。代码的另一部分中存在FSEventStreamScheduleWithRunLoop()和FSEventStreamStart()。感谢您的帮助。您知道如何在回调方法中检测文件夹中引发的文件夹事件类型吗?例如:文件重命名、文件创建等?对于现在遇到此答案的任何人(像我一样),相关链接现在似乎是: