Swift 2无法调用' FSEventStreamCreate'使用类型的参数列表

时间:2015-07-02 00:43:54

标签: ios xcode macos swift swift2

使用Swift 2和Xcode 7 beta 2(Build:7A121l)我使用FSEventStream API和@convention(c)得到了这个编译错误。

任何建议都很棒,谢谢!

internal class FileSystem {

init () {

    let allocator: CFAllocator? = kCFAllocatorDefault

    // Create FSEventStream and return valid FSEventStreamRef
    // Alias FSEventStreamCallback - CFunction

    typealias FSEventStreamCallback = @convention(c) (ConstFSEventStreamRef, UnsafeMutablePointer<Void>, UInt, UnsafeMutablePointer<Void>, UnsafePointer<FSEventStreamEventFlags>, UnsafePointer<FSEventStreamEventId>) -> Void

    let callback: FSEventStreamCallback = {
        (streamRef, clientCallBackInfo, numEvents, eventPaths, eventFlags, eventIds) -> Void in
        print ("changed")
        // handle file event
    }

    let context: UnsafeMutablePointer<FSEventStreamContext> = nil
    let pathsToWatch: CFArray = [NSHomeDirectory() + "/Dir"]
    let sinceWhen: FSEventStreamEventId = UInt64(kFSEventStreamEventIdSinceNow)
    let latency: CFTimeInterval = 1.0
    let flags: FSEventStreamCreateFlags = UInt32(kFSEventStreamCreateFlagNone)

    let eventStream = FSEventStreamCreate(
        allocator,
        callback,
        context,
        pathsToWatch,
        sinceWhen,
        latency,
        flags
    )

    FSEventStreamScheduleWithRunLoop(eventStream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode)
    FSEventStreamStart(eventStream)

}
}

1 个答案:

答案 0 :(得分:1)

您使用的是错误的签名(UInt不是Int):

typealias FSEventStreamCallback = @convention(c) (ConstFSEventStreamRef, UnsafeMutablePointer<Void>, Int, UnsafeMutablePointer<Void>, UnsafePointer<FSEventStreamEventFlags>, UnsafePointer<FSEventStreamEventId>) -> Void

编译好:

init () {

    let allocator: CFAllocator? = kCFAllocatorDefault

    // Create FSEventStream and return valid FSEventStreamRef
    // Alias FSEventStreamCallback - CFunction
    typealias FSEventStreamCallback = @convention(c) (ConstFSEventStreamRef, UnsafeMutablePointer<Void>, Int, UnsafeMutablePointer<Void>, UnsafePointer<FSEventStreamEventFlags>, UnsafePointer<FSEventStreamEventId>) -> Void
    let callback: FSEventStreamCallback = {
        (streamRef, clientCallBackInfo, numEvents, eventPaths, eventFlags, eventIds) -> Void in
        print ("changed")
        // handle file event
    }


    let context: UnsafeMutablePointer<FSEventStreamContext> = nil
    let pathsToWatch: CFArray = [NSHomeDirectory() + "/Dir"]
    let sinceWhen: FSEventStreamEventId = UInt64(kFSEventStreamEventIdSinceNow)
    let latency: CFTimeInterval = 1.0
    let flags: FSEventStreamCreateFlags = UInt32(kFSEventStreamCreateFlagNone)

    let eventStream = FSEventStreamCreate(
        allocator,
        callback,
        context,
        pathsToWatch,
        sinceWhen,
        latency,
        flags
    )

    FSEventStreamScheduleWithRunLoop(eventStream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode)
    FSEventStreamStart(eventStream)

}