如何在具有部署目标<的项目中仅引用iOS 4.0枚举? 4.0?

时间:2011-09-20 12:11:08

标签: objective-c ios cocoa-touch ios4

我正在尝试为iOS编写一个可以利用iOS 4.0功能的应用程序,但也可以使用早期版本的操作系统(3.1.3)。我已将部署目标设置为3.1.3,将Base SDK设置为4.3(最新)

具体来说,我试图利用拦截远程控制命令的能力。

下面链接的文档在解释如何(在运行时)检查是否存在类和方法时非常有用,但是当我尝试从UIEvent类引用枚举时仍然会出现编译器错误iOS 4.0及更高版本。

http://developer.apple.com/library/mac/#documentation/DeveloperTools/Conceptual/cross_development/Using/using.html#//apple_ref/doc/uid/20002000-SW3

以下是导致编译失败的代码部分:

- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent {   
    if (receivedEvent.type == UIEventTypeRemoteControl) {
        switch (receivedEvent.subtype) {
            case UIEventSubtypeRemoteControlTogglePlayPause:
                [self playPauseAction:nil];
                break;
            case UIEventSubtypeRemoteControlPreviousTrack:
                [self previousChapter:nil];
                break;
            case UIEventSubtypeRemoteControlNextTrack:
                [self nextChapter:nil];
                break;
            default:
                break;
        }
    }
}

编译器抱怨说:

error: 'UIEventTypeRemoteControl' undeclared (first use in this function)

UIEventTypeRemoteControl是枚举,直到4.0才定义 (来自UIEvent.h)

typedef enum {
    UIEventTypeTouches,
    UIEventTypeMotion,
    UIEventTypeRemoteControl,
} UIEventType;

typedef enum {
    // available in iPhone OS 3.0
    UIEventSubtypeNone                              = 0,

    // for UIEventTypeMotion, available in iPhone OS 3.0
    UIEventSubtypeMotionShake                       = 1,

    // for UIEventTypeRemoteControl, available in iOS 4.0
    UIEventSubtypeRemoteControlPlay                 = 100,
    UIEventSubtypeRemoteControlPause                = 101,
    UIEventSubtypeRemoteControlStop                 = 102,
    UIEventSubtypeRemoteControlTogglePlayPause      = 103,
    UIEventSubtypeRemoteControlNextTrack            = 104,
    UIEventSubtypeRemoteControlPreviousTrack        = 105,
    UIEventSubtypeRemoteControlBeginSeekingBackward = 106,
    UIEventSubtypeRemoteControlEndSeekingBackward   = 107,
    UIEventSubtypeRemoteControlBeginSeekingForward  = 108,
    UIEventSubtypeRemoteControlEndSeekingForward    = 109,
} UIEventSubtype;

那么如何阻止编译器抱怨呢?

另外 - 如何停止编译器警告someClass可能不响应someMethod(我在运行时检查该类是否确实响应该方法,然后调用它。)我想我可以关闭那个警告编译器设置 - 但在其他情况下它是一个有用的警告。

由于

1 个答案:

答案 0 :(得分:3)

好的 - 这是我发现的:

  1. 将deployment_target切换为4.3然后3.1.3会导致出现编译错误和警告。

  2. 一旦出现,您可以通过使用模拟器方案进行编译来摆脱它们。

  3. 完成后,您可以使用真实设备方案进行编译,错误和警告消失。