Question

I have a project that targets both Mac OS X 10.4 and 10.5, where 10.5 is the base SDK.

Some methods like -[NSString stringByReplacingOccurrencesOfString:withString] are unavailable in 10.4. I could just implement the functionality by hand. Another option would be to implement the method as a category, but that would mess with the 10.5 implementation and that's something I'd like to avoid.

So how do I implement such methods in 10.4 without messing up 10.5 and in such a way that I can take out the implementation easily when I decide to stop supporting 10.4?

Was it helpful?

Solution

I think you have to use +load and +initialize to load a method at runtime if the method doesn't already exists.

OTHER TIPS

if ([myString respondsToSelector: @selector(stringByReplacingOccurrencesOfString:withString:)])
{
  // 10.5 implementation
}
else
{
  // 10.4 implementation
}

Use a category, but put a tag on the method name; for example, stringByReplacingOccurrencesOfString_TigerCompatible:. In the implementation, call either Leopard's implementation or your own.

When you go Leopard-only, do a project search for “TigerCompatible”, then burninate all of those methods and un-tag all of their call sites.

Put all the missing implementation in categories in a bundle which is loaded on startup in main() if running under Tiger.

How about using a C preprocessor macro to insert the relevant methods if it's being built for 10.4? Maybe try doing something like this in a category, so those methods which don't exist on 10.4 are only included if it's being built for 10.4?

#if defined(MAC_OS_X_VERSION_10_4) && MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_4
// Put your implementations of the methods here
#endif

Do you need to support 10.4? If you're using 10.5 only methods in core parts of your app then it might be time to consider going 10.5 only.

Anyway, with the specific example given above, I suggest moving away from that and making a mutable copy of your string so you can use the similar method on NSMutableString which does work in 10.4

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top