Conditionally overriding a system method via categories in Objective-C?

Posted by adib on Stack Overflow See other posts from Stack Overflow or by adib
Published on 2010-03-18T14:56:57Z Indexed on 2010/03/18 17:01 UTC
Read the original article Hit count: 295

Filed under:
|
|

Hi

Is there a way to provide a method implementation (that bears the exact same name of a method defined by the framework) only when the method isn't already defined in the system? For example method [NSSomeClass someMethod:] exists only in Mac OS X 10.6 and if my app runs in 10.5, I will provide that method's definition in a category. But when the app runs in 10.6, I want the OS-provided method to run.

Background: I'm creating an app targeted for both 10.5 and 10.6. The problem is that I recently realized that method +[NSSortDescriptor sortDescriptorWithKey:ascending:] only exists in 10.6 and my code is already littered by that method call. I could provide a default implementation for it (since this time it's not too difficult to implement it myself), but I want the "native" one to be called whenever my app runs on 10.6. Furthermore if I encounter similar problems in the future (with more difficult-to-implement-myself methods), I might not be able to get away with providing a one-liner replacement.

This question vaguely similar to Override a method via ObjC Category and call the default implementation? but the difference is that I want to provide implementations only when the system doesn't already has one.

Thanks.

© Stack Overflow or respective owner

Related posts about cocoa

Related posts about objective-c