let firstHandler = NSGetUncaughtExceptionHandler()NSSetUncaughtExceptionHandler(onUncaughtException)let secondHandler = NSGetUncaughtExceptionHandler()if firstHandler == secondHandler { deBUGPrint("equal")}
更新==,也不是===有效@H_301_4@
在目标C中,我可以表达如下:@H_301_4@
NSUncaughtExceptionHandler *firstHandler = NSGetUncaughtExceptionHandler();NSSetUncaughtExceptionHandler(onUncaughtException);NSUncaughtExceptionHandler *secondHandler = NSGetUncaughtExceptionHandler();if (firstHandler == secondHandler) { NSLog(@"equal");}
提前致谢@H_301_4@ 通常,您无法在Swift中比较函数的相等性.
但是,NSSetUncaughtExceptionHandler返回一个
(@convention(c) (NSException) -> Swift.VoID)?
即(可选的)C兼容功能,并且这样的功能可以是
强制转换为(可选)原始指针
unsafeBitCast:@H_301_4@
let ptr1 = unsafeBitCast(firstHandler,to: Optional<UnsafeRawPointer>.self) let ptr2 = unsafeBitCast(secondHandler,to: Optional<UnsafeRawPointer>.self)
然后可以比较平等@H_301_4@
if ptr1 == ptr2 {.. }
一个独立的例子:@H_301_4@
func exceptionHandler1(exception : NSException) { }func exceptionHandler2(exception : NSException) { }NSSetUncaughtExceptionHandler(exceptionHandler1)let firstHandler = NSGetUncaughtExceptionHandler()NSSetUncaughtExceptionHandler(exceptionHandler2)let secondHandler = NSGetUncaughtExceptionHandler()let ptr1 = unsafeBitCast(firstHandler,to: Optional<UnsafeRawPointer>.self) print(ptr1 == ptr2) // false总结
以上是内存溢出为你收集整理的比较Swift中C函数的指针全部内容,希望文章能够帮你解决比较Swift中C函数的指针所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)