objective-ciosdeprecated

How to support deprecated methods in Objective-C


With each new OS a bunch of methods are declared deprecated. The strange thing for me is that if I want to still support iOS5 AND iOS6 I have to use BOTH the deprecated and the replacement method, like with UILabel's minimumScaleFactor over minimumFontSize.

If I replace myLabel.minimumFontSize to myLabel.minimumScaleFactor then my app will come crashing down in iOS5.

So I use an if with -respondsToSelector: to find out whether the OS is 5 or 6 and use minimumScaleFactor or minimumFontSize accordingly.

The problem is that I might have to write a bunch of ifs and respondsToSelectors in my code and that feels dumb.

Is there a better way to deal with deprecations?


Solution

  • Language independend solution: get the OS at the start of your app and set a global variable. Then, when neede query the variable for the OS version. You could do it in a case/switch statement to allow for easy extensability if changes in future versions occur.

    Pseudo code:

    switch iOSversion
        case < 6
            dothis
            break
        case <7
            dothat
            break
        case >7
            OS not supported ;)
    

    Technically it is the same thing as with the IFs, but your source would be shorter and more structured plus you don't have to query the OS version everytime, but once at the start of your app.