func callback(cf:CFNotificationCenter!,ump:UnsafeMutablePointer<VoID>,cfs:CFString!,up:UnsafePointer<VoID>,cfd:CFDictionary!) -> VoID {}
使用这个观察者:
CFNotificationCenteraddobserver(CFNotificationCenterGetDarwinNotifyCenter(),nil,self.callback,"myMESSage",CFNotificationSuspensionBehavior.DeliverImmediately)
导致此编译器出现错误:
“C函数指针只能通过引用”func“或者是文字闭包来形成”
只有一个全局函数或闭包(不捕获任何状态),
但不是一个实例方法.
所以这样做是有效的:
CFNotificationCenteraddobserver(CFNotificationCenterGetDarwinNotifyCenter(),{ (_,observer,name,_,_) in print("received notification: \(name)") },"myMessage",.DeliverImmediately)
但是由于闭包无法捕获上下文,因此您没有直接引用自身及其属性和实例方法.
例如,您不能添加
self.label.stringValue = "got it" // error: a C function pointer cannot be formed from a closure that captures context
在通知到达时,关闭内部更新UI.
有一个解决方案,但它有点复杂
Swift的严格型系统.
类似于Swift 2 – UnsafeMutablePointer<Void> to object,您可以将指针转换为
self到voID指针,将其作为观察器参数传递
到注册,并将其转换回到一个对象指针
回调.
class YourClass { func callback(name : String) { print("received notification: \(name)") } func registerObserver() { // VoID pointer to `self`: let observer = UnsafePointer<VoID>(Unmanaged.passUnretained(self).toOpaque()) CFNotificationCenteraddobserver(CFNotificationCenterGetDarwinNotifyCenter(),_) -> VoID in // Extract pointer to `self` from voID pointer: let mySelf = Unmanaged<YourClass>.fromOpaque( copaquePointer(observer)).takeUnretainedValue() // Call instance method: mySelf.callback(name as String) },.DeliverImmediately) } // ...}
封闭作为实例方法的“蹦床”.
指针是一个无法引用的参考,因此您必须确保
在对象被释放之前观察者被移除.
Swift 3的更新:
class YourClass { func callback(_ name : String) { print("received notification: \(name)") } func registerObserver() { // VoID pointer to `self`: let observer = UnsafeRawPointer(Unmanaged.passUnretained(self).toOpaque()) CFNotificationCenteraddobserver(CFNotificationCenterGetDarwinNotifyCenter(),_) -> VoID in if let observer = observer,let name = name { // Extract pointer to `self` from voID pointer: let mySelf = Unmanaged<YourClass>.fromOpaque(observer).takeUnretainedValue() // Call instance method: mySelf.callback(name.rawValue as String) } },"myMessage" as CFString,.deliverImmediately) } // ...}
参见How to cast self to UnsafeMutablePointer<Void> type in swift了解更多信息关于对象指针和C指针之间的“桥接”.
总结以上是内存溢出为你收集整理的swift – 如何使用实例方法作为只需func或文字关闭的函数的回调全部内容,希望文章能够帮你解决swift – 如何使用实例方法作为只需func或文字关闭的函数的回调所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)