swift – 如何使用实例方法作为只需func或文字关闭的函数的回调

在“ViewController. swift”中,我创建了这个回调:
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“或者是文字闭包来形成”

回调是指向C函数的指针,在Swift中可以通过
只有一个全局函数或闭包(不捕获任何状态),
但不是一个实例方法.

所以这样做是有效的:

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 正式开源!Swift 团队很高兴宣布 Swift 开始开源新篇章。自从苹果发布 Swfit 编程语言,就成为了...
快,快,快!动动您的小手,分享给更多朋友! 苹果去年推出了全新的编程语言Swift,试图让iOS开发更简单...
开发者(KaiFaX) 面向开发者、程序员的专业平台! 和今年年初承诺的一样,苹果贴出了Swift语言的源码,...
本文由@Chun发表于Chun Tips :http://chun.tips/blog/2014/12/11/shi-yong-swift-gou-jian-zi-ding-yi...
本文由CocoaChina译者leon(社区ID)翻译 原文:THE RIGHT WAY TO WRITE A SINGLETON 在之前的帖子里聊过...
本文由CocoaChina译者leon(社区ID)翻译 原文:THE RIGHT WAY TO WRITE A SINGLETON 在之前的帖子里聊过...