有条件的进口框架 [英] conditionally import framework
问题描述
首先我确定这是一个简单的问题。
well to begin with I'm sure this is a simple question.
我正在使用iAd Framework开发iPhone应用程序,该应用程序仅适用于iOS 4.0或更高。
I am developing an iPhone app with the iAd Framework, which only runs for iOS 4.0 or higher.
不过,我想选择一个iPhone OS 3.0部署目标,这会导致一切崩溃。
Still, I wanna choose a iPhone OS 3.0 deployment target, which causes everything to crash.
- 我如何有条件地包含iAd框架?
...我的意思是,它会是这样的:
... if([[UIDevice currentDevice] systemVersion]> = 4.0])#import
显然这不起作用,因为我不知道正确的语法。另外:
Obviously this won't work because I don't know the correct syntax. Also:
- 如何有条件地声明AdView *变量?
- 我如何有条件地处理这个AdView *变量在我的实现文件中。
如果你们能帮助我,我会留下非常深刻的印象。
If you guys could help me, I will be very well impressed.
谢谢
推荐答案
您无需更改包含,您需要使iAd(或任何其他新框架)弱连接:
You don't need to change your include, you need to make the iAd (or any other new framework) linked weakly:
在目标中,在链接框架中找到iAd并将其角色从必需更改为弱。
In your target, find iAd in the linked frameworks and change its "Role" from "Required" to "Weak".
要有条件地处理变量,请使用 NSClassFromString
函数,如下所示:
To handle the variable conditionally, use NSClassFromString
function, like this:
Class AdClass = NSClassFromString(@"ADBannerView");
if(AdClass) {//if the class exists
ADBannerView* myAd = [[AdClass alloc] initWithFrame:CGRectZero];
// do something with the ad
}
如果操作系统早于iOS 4.0, AdClass
将为 nil
,代码将不会执行。请注意,使用 ADBannerView *
作为变量的类型不应该导致任何问题,因为它只是编译器的提示并且与相同编译后id
。
If OS is older than iOS 4.0, AdClass
will be nil
and the code won't execute. Note that using ADBannerView*
as the type of the variable shouldn't cause any problems, as it's just a hint for a compiler and is the same as id
after compilation.
这篇关于有条件的进口框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!