开发者

Objective-C/iOS - Test for existence of C function?

Is there a way to test for the existence of C functions in Objective-C? I'm looking for something like "respondsToSelector," but for C functions.

More specifically, I'm trying to test fo开发者_StackOverflow中文版r the existence of "UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque>, CGFloat scale)" in iOS.

Thanks.


if(&UIGraphicsBeginImageContextWithOptions != NULL)
    UIGraphicsBeginImageContextWithOptions();
    else NSLog(@"No UIGraphicsBeginImageContextWithOptions function");


Yes, see the documentation on weak linking.

It'll work out to checking if the function pointer is NULL before calling the function.


Since UIGraphicsBeginImageContextWithOptions is only present in iOS 4.0 and later, you can check the iOS target version with the __IPHONE_OS_VERSION_MAX_ALLOWED macro:

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000
    UIGraphicsBeginImageContextWithOptions(...);
#else
    // Function not available, fail gracefully
#endif
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜