Problem when overriding new Apple methods with my categories, what is the best solution?
Problem when overriding new Apple methods with my categories, what is the best solution?
- Subject: Problem when overriding new Apple methods with my categories, what is the best solution?
- From: Aurélien Hugelé <email@hidden>
- Date: Fri, 15 Dec 2006 11:14:08 +0100
Hi List!
I'm facing a common problem.
My company and I have developped many cocoa frameworks during the
last 3 years used in about 4 applications. One of them in particular
called "GumiFoundationKit" is almost only a collection of (objC)
categories on FoundationKit classes.
During the years, we have attached a lot of importance to use Cocoa
method naming conventions. Now, in particular with Leopard, our
categories conflict with Apple method names (no one will blame them
to finally have implemented such desired methods, but 5 years just
for some basic ones it a little bit too long ;-) )
Just for clarity, here is a sample of categories we implemented, and
that Apple finally added in 10.5:
NSThread:
+(BOOL)isMainThread;
-(BOOL)isMainThread;
+(NSThread*)mainThread;
NSSet/NSMutableSet
-(NSSet*)filteredSetUsingPredicate:(NSPredicate*)aPredicate;
-(void)filterUsingPredicate(NSPredicate*)aPredicate;
and some others I don't remember...
I'm looking for an ideal solution:
1/ use the *very same* framework build for 10.5 and 10.X, I do not
want to ship 2 versions of each app
2/ do not change our method names, i would like to have my method
names to be as consistent as possible, i hate using nasty things like
_myFilterUsingPredicate: or whatever. It is unreadable, inconsistent
and requires much changes on big applications. Cocoa method names are
just so great and natural.
3/ use Apple implementations as much as possible, i'm pretty
confident most of them are faster or less buggy than ours.
Currently the best solution I have thought about, is to move my
conflicting categories in a bundle that will be loaded only if the
running process is not on 10.5. But I would like the bundle to
*remain inside the framework*, and if possible the loading to be
"automatic" for each applications (I mean that i don't want to load
the bundle manually from inside the *hosting application code*)
How could I achieve automatic bundle loading *from a framework*?
I know that a category should never override the +(void)initialize
method, but to demonstrate what i would Ideally like to do
i would have reimplemented the +(void)initialize method of NSThread
and NSSet/NSMutableSet like this:
+(void)initialize
{
// test if we are running on OS X 10.4 or less and that the bundle
containing categories is not loaded
if( ! CURRENT_OS_IS_10_5 &&
! [[NSBundle
bundleWithIdentifier:MY_BUNDLE_IDENTIFIER_CONTAINING_THE_DESIRED_CATEGOR
IES] isLoaded])
{
// load the bundle once, so that FoundationKit classes get their
categories added on the fly
[[NSBundle
bundleWithIdentifier::MY_BUNDLE_IDENTIFIER_CONTAINING_THE_DESIRED_CATEGO
RIES] load];
}
}
As i said, this can not be done because overriding the +initialize
method is... well... not recommended :)
Has anybody a CLEAN solution to that problem, something that would
match the three points i developed above?
Thanks for your help, i'm pretty sure this problem will be more
common as the Cocoa developer code base will grow!
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden