Objective-C检查是否定义了结构 [英] Objective-C Check if Structs is defined

查看:121
本文介绍了Objective-C检查是否定义了结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的iOS应用程序可以使用可选的外部第三方库.

My iOS application can use an optional external 3rd party library.

我考虑使用此答案(弱链接-检查类是否存在并使用该类),并在执行特定于该库的代码之前检测该类是否存在.

I thought of using this answer (Weak Linking - check if a class exists and use that class) and detect if the class exists before executing code specific to this library.

但是,我发现此外部库不是作为Objective-C类编写的,而是作为C STRUTS和函数编写的.

However, I found out that this external library is not written as Objective-C classes, but rather as C STRUTS and functions.

是否有类似的技术可以让我检查C Strut或函数是否存在?还是一些更好的选择,以查看运行时该库是否存在?

Is there a similar technique that would allow me to check if a C Strut or function exists? Or some better alternative to see if this library is present at runtime?

推荐答案

struct是编译时工件.他们告诉编译器如何布置内存区域.完成此操作后,struct就不再需要了.与具有元数据的Objective-C类不同,structs没有运行时状态.这就是为什么无法在运行时检测到它们的原因.

structs are compile-time artifacts. They tell the compiler how to lay out a region of memory. Once that is done, structs become unnecessary. Unlike Objective-C classes which have metadata, structs have no runtime presence. That is why it is not possible to detect them at runtime.

您可以通过调用

You can check if a dynamic library is present by calling dlopen, and passing its path:

void *hdl = dlopen(path_to_dl, RTLD_LAZY | RTLD_LOCAL);
if (hdl == NULL) {
    // The library failed to load
    char *err = dlerror(); // Get the error message
} else {
    dlclose(hdl);
}

如果dlopen返回NULL,则无法加载库.您可以通过调用 dlerror .您需要调用 dlclose .

If dlopen returns NULL, the library cannot be loaded. You can get additional info by calling dlerror. You need to call dlclose after you are done.

这篇关于Objective-C检查是否定义了结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆