代码之家  ›  专栏  ›  技术社区  ›  Duck

iPhone-定位不兼容代码

  •  1
  • Duck  · 技术社区  · 15 年前

    我有一个代码,其中有两个目标使用iOS 4特有的函数,其他目标则与3.0+兼容。

    我想对代码做最后的修改,看看是否一切正常。换句话说,看看在为ios 3.x目标编译时是否没有用于调用ios4的函数。

    在编译过程中,有没有一种方法可以列出被调用的任何不属于目标所需版本的函数?

    事先谢谢。

    2 回复  |  直到 15 年前
        1
  •  1
  •   justin    15 年前

    1)一直向上打开编译器警告

    2)将警告视为错误

    3)将基本/目标SDK更改为您将支持的最早版本

    4)清洁

    5)建造

    隐式函数和未声明的选择器等内容现在应该会产生错误。

    为了避免今后出现错误,请为您需要的函数或objc方法创建一个填充静态库。这个填充程序将实现代码的两种变体,并最终根据最新的头部构建。它将包含条件运行时检查。对于objc类方法,可以伪造、使用类别,并在生成任何警告的地方使用类别实现。

    要说明如何使用两个版本的代码:

    /*
    illustration:
    - sortByCoordinates: was added in OS 4, but we need to implement or approximate it for our projects targeting OS 3 (or throw out an alert in some cases)
    - as long as you have to support OS 3 and build against the SDKs from time to time, the compiler will produce errors or warnings when a selector has not been declared, or if an object may not respond to a selector (assuming you have directed the compiler to inform you of this)
    - so we put our conditionals/runtime checks in a library here, and update our calls from sortByCoordinates: to mon_sortByCoordinates:
    */
    
    - (void)mon_sortByCoordinates:(EECoordinate*)coordinates
    {
    /* MONLib_OSVersion_4_0 implies some constant, indicating OS 4 */
    /* MONLibGetCurrentRuntimeVersion() is a call we make at runtime which returns the OS version, such as MONLib_OSVersion_4_0 or MONLib_OSVersion_3_2 */
        if (MONLib_OSVersion_4_0 <= MONLibGetCurrentRuntimeVersion()) {
        /* they are using OS 4 (or greater) - call the OS version */
            [self sortByCoordinates:coordinates];
        }
        else {
        /*
        %%%%%% < else implement our approximation here >
        */
        }
    }
    

    最后,由于objc的动态特性和通常编写objc程序的方式,编译器不能为您捕捉所有的信息。有时候更详细一点会有帮助。简单示例:

    @interface MONBox
    /* added in version 4 */
    - (NSUInteger)count;
    @end
    
    NSUInteger beispiel() {
        if (0) {
        /* less safe -- the compiler will not flag this */
        /* if [myNSArrayInstace objectAtIndex:0] is a MONBox then this will go undetected since the selector count may be matched to -[NSArray count] */
            return [[myNSArrayInstaceOfMONBoxObjects objectAtIndex:0] count];
        }
        else {
        /* more safe -- the compiler will flag this */
            MONBox * box = [myNSArrayInstaceOfMONBoxObjects objectAtIndex:0];
            return [box count];
        }
    }
    
        2
  •  0
  •   Daniel Dickison    15 年前

    据我所知,我不认为有一种自动化的方法可以做到这一点。为了这个目的,我保留了一个旧的第一代iPhone——我将在设备上运行这个应用程序,看看会发生什么崩溃。不太理想,但对于较小的应用程序来说,它可以正常工作。