Nate Thorn Nate Thorn - 3 months ago 20
Objective-C Question

Portable code for deprecated userInfo dictionary key (Cocoa)

In Mac OSX 10.6, the NSErrorFailingURLStringKey userInfo dictionary key is deprecated in favor of NSURLErrorFailingURLStringErrorKey. I am trying to write my program to be portable to both Mac OSX 10.5 and 10.6. For the time being, I'm just using the old key--but my compiler is giving me annoying deprecated warnings.

// The following causes deprecation warnings
[[error userInfo] objectForKey:NSErrorFailingURLStringKey]

// But this one won't work on OSX 10.5
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]


What is the best way to write portable code to handle deprecated userInfo dictionary keys?

Answer

You can use preprocessor directives like so:

#if defined(MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
    [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]
#else
    [[error userInfo] objectForKey:NSErrorFailingURLStringKey]
#endif