简体   繁体   English

静态库iOS中的链接弱

[英]Weak linking in static library iOS

We have a static framework that we have built and it depends upon an SDK that we use to access ID's. 我们已经建立了一个静态框架,它依赖于我们用来访问ID的SDK。 This SDK has come out with a new version which has a new interface. 该SDK已发布了具有新界面的新版本。

Currently the user would add our framework and the dependency SDK and everything would work fine. 当前,用户将添加我们的框架和依赖项SDK,并且一切正常。 Now we want the user to be able to add our framework and either the old or new SDK and I'm not sure how to do this without creating multiple targets which we would rather not do. 现在,我们希望用户能够添加我们的框架以及旧的或新的SDK,并且我不确定如何在不创建多个我们不希望这样做的目标的情况下执行此操作。 With the way I have it setup the user has to add our SDK and both the old and new SDK's. 使用我设置的方式,用户必须添加我们的SDK以及旧的和新的SDK。

I have a protocol SDKProtocol which is implemented by two classes NewSDKServices and OldSDKServices . 我有一个协议SDKProtocol ,它由两个类NewSDKServicesOldSDKServices

NewSDKServices won't compile unless the new SDK is present and OldSDKServices won't compile unless the old SDK is present. NewSDKServices将无法编译,除非新的SDK存在并且OldSDKServices将无法编译,除非老的SDK是存在的。 I figure this is OK since it is a precompiled framework and we can decided at runtime which to use. 我认为这是可以的,因为它是一个预编译的框架,我们可以在运行时决定使用哪个框架。

Then I'd like to be able to do something like this, maybe with a ifdef to import and initialize the correct service. 然后,我希望能够执行类似的操作,也许可以使用ifdef导入并初始化正确的服务。

if (useNewSDK) {
    _sdkService = [[NewSDKServices alloc] init];
} else {
    _sdkService = [[OldSDKServices alloc] init];
}

I've thought about weak linking these libraries but not exactly sure how it would work since you can't static link frameworks to other static frameworks. 我曾考虑过弱链接这些库,但由于不能将静态链接框架静态链接到其他静态框架,因此无法完全确定它的工作方式。 I'm hoping for some direction. 我希望有方向。

Both SDK's have a few headers and a .a . 这两个SDK都有一些标头和.a

I know you said you didn't want two targets. 我知道您说过您不想要两个目标。 To be honest this is probably the best option as you are being explicit and up-front about the requirements so nobody can get confused easily. 坦白地说,这可能是最好的选择,因为您对要求很明确并且很先行,因此没有人会很容易混淆。 There are many ways to manage this by re-using source files and even having the way you suggested 有很多方法可以通过重新使用源文件甚至使用您建议的方式来管理此问题

if (useNewSDK) {
    _sdkService = [[NewSDKServices alloc] init];
} else {
    _sdkService = [[OldSDKServices alloc] init];
}

would be 将会

#ifdef USE_SDK_NEW
    _sdkService = [[LinkedSDK alloc] init];
#else //USE_SDK_NEW
    _sdkService = [[LinkedSDK alloc] init];
#endif //USE_SDK_NEW 

However... if you really want to do it the other way and it works out, you must try to use weak linking. 但是...如果您真的想以其他方式做到这一点并且可以解决问题,则必须尝试使用​​弱链接。

If using weak linking it generally requires NSClassFromString(...) to access stuff. 如果使用弱链接,则通常需要NSClassFromString(...)来访问内容。 You would need to determine the availability of the SDK, hopefully someone has either added the info to the static classes like [NSClassFromString("TheSdk") performSelector: @"GetVersion"] or use the knowledge that other classes exist or do not exist. 您将需要确定SDK的可用性,希望有人将信息添加到了[NSClassFromString("TheSdk") performSelector: @"GetVersion"]类的静态类中,或者利用了其他类是否存在的知识。 If the set of classes between two version is the same, you'd fallback to querying selectors or members. 如果两个版本之间的类集相同,则您将退回到查询选择器或成员。

Again this would not be pretty. 再次,这将不是很漂亮。 You could make it less ugly by using the strategy pattern if you are familiar and have a strategy for the old SDK and the new SDK, and try to weak-link the classes without causing the compiler to require them. 如果熟悉并且对旧的SDK和新的SDK有策略,则可以使用策略模式来使其变得不那么难看,并尝试弱链接这些类而不会导致编译器要求它们。

If you are distributing yourself as a pod, you could create two targets and use the version to differentiate the required other SDK. 如果您将自己作为Pod进行分发,则可以创建两个目标,并使用该版本来区分所需的其他SDK。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM