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

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

问题描述:

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

Using Swift 2 and Xcode 7 beta 2 (Build: 7A121l) I get this compile error using FSEventStream API and @convention(c).

任何建议都是太棒了,谢谢!

Any advice would be awesome, thanks!

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)

}
}


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

You are using the wrong signature (UInt not 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)

}