Swift 2 无法使用类型的参数列表调用 'FSEventStreamCreate'
Swift 2 Cannot invoke 'FSEventStreamCreate' with an argument list of type
使用 Swift 2 和 Xcode 7 beta 2(内部版本: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)
}
}
您使用了错误的签名(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)
}
使用 Swift 2 和 Xcode 7 beta 2(内部版本: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)
}
}
您使用了错误的签名(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)
}