On a past project (pre-iOS 4.0), I wrote the following category method on NSSortDescriptor:
+ (id)sortDescriptorWithKey:(NSString *)key ascending:(BOOL)ascending;
When Apple released the iOS SDK 4.0, it included the exact same method (which presumably does exactly the same thing). Is it possible to write a category that is only added to the runtime either if you’re running a particular OS version, or probably more to the point, if there isn’t already a method declared with the same signature?
In this case, it’s probably safe to override the sortDescriptorWithKey:ascending: method with a category, which will give both iOS 3 and iOS 4 support, since my version will almost certainly do the same thing. I’d still prefer not to mess with system defined methods if possible, due to the (unlikely) possibility of breaking things in edge cases.
Not directly, no.
The way I’d recommend doing this would be to have a
my_sortDescriptorWithKey:which can then check if the class responds tosortDescriptorWithKey:and uses that if it does, otherwise use your own implementation.