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

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

本文介绍了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)

}

这篇关于Swift 2无法使用类型的参数列表调用'FSEventStreamCreate'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 03:00