开发者

Import class only if it's available (e.g. UIPopOverControllerDelegate only on >= 3.2)

开发者 https://www.devze.com 2023-04-07 15:56 出处:网络
I\'ve got a class which is implementing the UIPopOverControllerDelegate: #if ios_atleast_32 @interface MyClass : UIViewController <UIPopoverDelegate>

I've got a class which is implementing the UIPopOverControllerDelegate:

#if ios_atleast_32
@interface MyClass : UIViewController <UIPopoverDelegate>
#elsif
@interface MyClass : UIViewController
#endif

Is it possible to make determine at runtime 开发者_StackOverflowif the class is available and therefore should be used as delegate? The same for imports:

#if ios_4_available
#import "MyClassWhichIsUsingIos4Stuff.h"
#endif


You're building against the latest SDK so you can always #import new stuff and don't need any preprocessor macros there. The same is true for the protocol.

Just make sure that before using classes that are not available on all your supported OS versions you check whether that class exists or your app will crash:

Class someNewClass = NSClassFromString(@"SomeNewClass");
if (someNewClass) {
    ...
}
else {
    ...
}

In newer versions of the SDK (don't ask me what exactly is the requirement) you can also do something like this:

if ([SomeNewClass class]) {
    ...
}
else {
    ...
}


You can simply implement the protocol regardless of which iOS version will be used at runtime, and it will work fine.


as the two above mentioned, it's no problem to import classes - but UIKit has to be weakly linked... that was the missing point in my case!

0

精彩评论

暂无评论...
验证码 换一张
取 消