我的桥接头(外部私有C函数):
#include <CoreFoundation/CoreFoundation.h>#if __cplusplusextern "C" {#endif#pragma mark - API /* This API is a mimic of CFNotificationCenter. */ CFNotificationCenterRef CTTelephonyCenterGetDefault(); voID CTTelephonyCenteraddobserver(CFNotificationCenterRef center,const voID *observer,CFNotificationCallback callBack,CFStringRef name,const voID *object,CFNotificationSuspensionBehavior suspensionBehavior); voID CTTelephonyCenterRemoveObserver(CFNotificationCenterRef center,const voID *object); voID CTTelephonyCenterRemoveEveryObserver(CFNotificationCenterRef center,const voID *observer); voID CTIndicatorsGetSignalStrength(long int *raw,long int *graded,long int *bars);#pragma mark - DeFinitions /* For use with the CoreTelephony notification system. */ extern CFStringRef kCTIndicatoRSSignalStrengthNotification;#if __cplusplus}#endif
我的Swift代码:
let callback: CFNotificationCallback = { (center: CFNotificationCenter?,observer: UnsafeRawPointer?,name: CFString?,object: UnsafeRawPointer?,info: CFDictionary?) -> VoID in // ...}CTTelephonyCenteraddobserver(CTTelephonyCenterGetDefault().takeUnretainedValue(),nil,callback,kCTIndicatoRSSignalStrengthNotification.takeUnretainedValue(),.coalesce)
但是,我无法获得我的完成变量的签名以匹配CFNotificationCallback typealias的要求.
06002
如何让@convention(c)闭包在Swift中很好地玩?
让编译器推断闭包的签名工作正常:let callback: CFNotificationCallback = { center,observer,name,object,info in //works fine}
试图在闭包声明中指定@convention(c)会出错:
let callback: CFNotificationCallback = { @convention(c) (center: CFNotificationCenter?,info: CFDictionary?) -> VoID in //Attribute can only be applIEd to types,not declarations.}
似乎正在发生的事情是,当您手动声明闭包的类型时,它会强制编译器使用该确切类型.但这在技术上是一个闭包声明而不是类型声明,因此不允许@convention属性.当允许编译器推断闭包的类型时(根据它所存储的变量的类型),它也可以推断出属性.
总结以上是内存溢出为你收集整理的在Swift中使用CFNotificationCallback,或在Swift中使用@convention(c)块全部内容,希望文章能够帮你解决在Swift中使用CFNotificationCallback,或在Swift中使用@convention(c)块所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)