Swift   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了比较Swift中C函数的指针大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我正在寻找一种方法来比较指向c函数的指针.例如.: let firstHandler = NSGetUncaughtExceptionHandler() NSSetUncaughtExceptionHandler(onUncaughtException) let secondHandler = NSGetUncaughtExceptionHandler() if firstHandler ==
我正在寻找一种方法来比较指向c函数的指针.例如.:
let firstHandler = NSGetUncaughtExceptionHandler()

NSSetUncaughtExceptionHandler(onUncaughtException)
let secondHandler = NSGetUncaughtExceptionHandler()

if firstHandler == secondHandler {
    debugPrint("equal")
}
@H_502_3@更新==,也不是===有效

@H_502_3@在目标C中,我可以表达如下:

NSUncaughtExceptionHandler *firstHandler = NSGetUncaughtExceptionHandler();

NSSetUncaughtExceptionHandler(onUncaughtException);
NSUncaughtExceptionHandler *secondHandler = NSGetUncaughtExceptionHandler();

if (firstHandler == secondHandler) {
    NSLog(@"equal");
}
@H_502_3@提前致谢

通常,您无法在Swift中比较函数相等性.
但是,NSSetUncaughtExceptionHandler返回一个
(@convention(C) (NSException) -> Swift.Void)?
@H_502_3@即(可选的)C兼容功能,并且这样的功能可以是
强制转换为(可选)原始指针
unsafeBitCast:

let ptr1 = unsafeBitCast(firstHandler,to: Optional<UnsafeRawPointer>.self) 
let ptr2 = unsafeBitCast(secondHandler,to: Optional<UnsafeRawPointer>.self)
@H_502_3@然后可以比较平等

if ptr1 == ptr2 {.. }
@H_502_3@一个独立的例子:

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函数的指针所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。