开发者

iphone - locating incompatible code

I have a code with two targets that uses functions specific to iOS 4 and others compatible with 3.0+.

I would开发者_开发问答 like to make a final revision on the code to see if everything is fine. In other words, to see if there's no function meant for iOS4 being called when compiling for iOS 3.x target.

Is there a way to list, during compilation, any functions being called that doesn't belong to the desired version for the target?

thanks in advance.


1) turn the compiler warnings all the way up

2) treat warnings as errors

3) change the base/target SDK to the earliest version you will support

4) clean

5) build

things such as implicit functions and undeclared selectors should now generate errors.

to avoid errors going forward, create a shim static library for the functions or objc methods you will need. this shim will implement two variations of the code, and will ultimately build against the latest headers. it will contain conditional runtime checks. for the objc class methods you can fake, use categories and use the category implementation where any warning is generated.

to illustrate using two versions of the code:

/*
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 >
    */
    }
}

finally, the compiler can't catch everything for you, due to objc's dynamic nature and the way objc programs are often written. sometimes it helps to be more verbose. simple example:

@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];
    }
}


I don't think there's an automated way to do this, as far as I know. I keep an old first-gen iPhone around for this purpose -- I'll run the app on the device and see what crashes. Not ideal but it works OK for smaller apps.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜