检查Swift中是否存在全局函数

时间:2021-08-17 09:04:53

Is it possible to detect if some global function (not class method) is defined (in iOS)? Something like respondsToSelector in a class...

是否可以检测是否定义了某些全局函数(非类方法)(在iOS中)?像课堂上的respondsToSelector ...

2 个解决方案

#1


6  

Swift currently does not support looking up global functions.

Swift目前不支持查找全局功能。

For C functions (most global functions from Apple's frameworks are C functions) there are at least two ways:

对于C函数(Apple的框架中的大多数全局函数都是C函数),至少有两种方法:

  • using a weakly linked symbol
  • 使用弱链接符号
  • the dynamic linker API: dlopen
  • 动态链接器API:dlopen

Both check dynamically (at runtime) if a symbol can be found.

如果可以找到符号,则动态检查(在运行时)。

Here's an example that checks if UIGraphicsBeginImageContextWithOptions (introduced with iOS 4) is available:

这是一个检查UIGraphicsBeginImageContextWithOptions(iOS 4引入)是否可用的示例:

void UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale) __attribute__((weak));

static inline BOOL hasUIGraphicsBeginImageContextWithOptions() {
    return UIGraphicsBeginImageContextWithOptions != NULL;
}

Here's the same check, using dlsym:

这是使用dlsym的相同检查:

#import <dlfcn.h>

static inline BOOL hasUIGraphicsBeginImageContextWithOptions() {
    return dlsym(RTLD_SELF, "UIGraphicsBeginImageContextWithOptions") != NULL;
}

The advantage of using dlsym is that you don't need a declaration and that it's easily portable to Swift.

使用dlsym的优点是你不需要声明,并且它可以轻松移植到Swift。

#2


0  

No, it's not possible in Swift.

不,这在Swift中是不可能的。

Even respondsToSelector uses the Obj-C runtime and can be used only for functions available in Obj-C.

甚至respondsToSelector也使用Obj-C运行时,并且只能用于Obj-C中可用的函数。

#1


6  

Swift currently does not support looking up global functions.

Swift目前不支持查找全局功能。

For C functions (most global functions from Apple's frameworks are C functions) there are at least two ways:

对于C函数(Apple的框架中的大多数全局函数都是C函数),至少有两种方法:

  • using a weakly linked symbol
  • 使用弱链接符号
  • the dynamic linker API: dlopen
  • 动态链接器API:dlopen

Both check dynamically (at runtime) if a symbol can be found.

如果可以找到符号,则动态检查(在运行时)。

Here's an example that checks if UIGraphicsBeginImageContextWithOptions (introduced with iOS 4) is available:

这是一个检查UIGraphicsBeginImageContextWithOptions(iOS 4引入)是否可用的示例:

void UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale) __attribute__((weak));

static inline BOOL hasUIGraphicsBeginImageContextWithOptions() {
    return UIGraphicsBeginImageContextWithOptions != NULL;
}

Here's the same check, using dlsym:

这是使用dlsym的相同检查:

#import <dlfcn.h>

static inline BOOL hasUIGraphicsBeginImageContextWithOptions() {
    return dlsym(RTLD_SELF, "UIGraphicsBeginImageContextWithOptions") != NULL;
}

The advantage of using dlsym is that you don't need a declaration and that it's easily portable to Swift.

使用dlsym的优点是你不需要声明,并且它可以轻松移植到Swift。

#2


0  

No, it's not possible in Swift.

不,这在Swift中是不可能的。

Even respondsToSelector uses the Obj-C runtime and can be used only for functions available in Obj-C.

甚至respondsToSelector也使用Obj-C运行时,并且只能用于Obj-C中可用的函数。