繁体   English   中英

iOS:检查#define中的系统版本并执行不同的操作,编译错误

[英]iOS: check system version in #define and do different stuff, compiler error

我使用MKReverseGeocoder for iOS <5和CLGeocoder for iOS> = 5,但是因为MKReverseGeocoder被弃用而得到警告,然后我尝试做类似的事情:

#define SYSTEM_LOWER_THAN_5 ([[[UIDevice currentDevice] systemVersion] compare:@"5.0" options:NSNumericSearch] == NSOrderedAscending)

#if SYSTEM_LOWER_THAN_5
@interface TelstraLocationService () <MKReverseGeocoderDelegate>
#else
@interface TelstraLocationService ()
#endif

但是我收到一个错误:

预处理器表达式开始时的令牌无效

这是由只能在运行时评估的宏引起的。 有没有办法摆脱警告(不改变部署目标)?

如果您的目标是简单地摆脱编译器警告(因为您已经解决了任何潜在的问题并明确决定忽略它),请使用以下编译器指令包围触发代码:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
    // some code that uses something deprecated
#pragma clang diagnostic pop

您希望尽可能具体,以避免无意中忽略其他有效的弃用警告。 如果你之前和之后有另一个协议,那么只围绕MKReverseGeocoderDelegate ,否则只围绕整个@interface块:

// option 1
@interface TelstraLocationService () <
    SomeProtocol

    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Wdeprecated-declarations"
          MKReverseGeocoderDelegate
    #pragma clang diagnostic pop

    AnotherProtocol>

// option 2
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"

@interface TelstraLocationService () <MKReverseGeocoderDelegate>

#pragma clang diagnostic pop

围绕对reverseGeocoder:didFindPlacemark:任何调用reverseGeocoder:didFindPlacemark:reverseGeocoder:didFailWithError:使用相同的编译器指令来为它们静默弃用警告。

暂无
暂无

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

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