On Aug 20, 2012, at 19:41 , cocoa-dev-requ...@lists.apple.com wrote:
> Date: Mon, 20 Aug 2012 15:16:43 -0700
> From: Greg Parker <gpar...@apple.com>
> Message-ID: <4cc28fb2-6935-4829-8606-0db9cf6ac...@apple.com>
> 
> On Aug 20, 2012, at 2:02 PM, Jens Alfke <j...@mooseyard.com> wrote:
>> On Aug 20, 2012, at 12:52 PM, I wrote:
>>> My first thought was to declare some categories on NSArray and NSDictionary 
>>> to define those methods, but that's likely to cause issues when iOS 6 comes 
>>> out and already includes those methods — I'll get compile errors (which I 
>>> can work around with #ifdefs) but also a warning at launch time because the 
>>> methods are declared in two places.
> 
> IIRC there are some subscript-able classes that libarclite does not upgrade 
> (NSOrderedSet, maybe?) so you still need to test thoroughly on older 
> deployment targets.


I can confirm NSOrdered[Mutable]Set isn't covered by libarclite. Here's what 
I'm using for that (easily extended to other classes:

@interface NSOrderedSet()
- (id)objectAtIndexedSubscript:(NSUInteger)idx;
@end

@interface NSMutableOrderedSet()
- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx;
@end

static void PatchClass(Class class ,SEL newsel, SEL oldsel) {
        id instance = [[class alloc] init];
        class = [instance class];
        [instance release];
        if (![class instancesRespondToSelector:newsel]) {
                if ([class instancesRespondToSelector:oldsel]) {
                        Method method = RBXCALL(class_getInstanceMethod)(class, 
oldsel);
                        if (method) {
                                IMP imp = 
RBXCALL(method_getImplementation)(method);
                                if (imp) {
                                        const char* enc = 
RBXCALL(method_getTypeEncoding)(method);
                                        if (enc) {
                                                RBXCALL(class_addMethod)(class, 
newsel, imp, enc);
                                        }
                                }
                        }
                }
        }
}

static void SetObjectSwapped(id myself, SEL _cmd, id obj,NSInteger idx) {
        [myself replaceObjectAtIndex:idx withObject:obj];
}

static void PatchClassSwap(Class class, SEL newsel) {
        id instance = [[class alloc] init];
        class = [instance class];
        [instance release];
        if (![class instancesRespondToSelector:newsel]) {
                if ([class 
instancesRespondToSelector:@selector(replaceObjectAtIndex:withObject:)]) {
                        RBXCALL(class_addMethod)(class, newsel, 
(IMP)SetObjectSwapped, "v@:@q");
                }
        }
}

...and early in the app's startup I call:

        PatchClass([NSOrderedSet class], @selector(objectAtIndexedSubscript:), 
@selector(objectAtIndex:));
        PatchClass([NSMutableOrderedSet class], 
@selector(objectAtIndexedSubscript:), @selector(objectAtIndex:));
        PatchClassSwap([NSMutableOrderedSet class], 
@selector(setObject:atIndexedSubscript:));

This runs fine on both 10.7 and 10.8, so it should also work on iOS5 and iOS6.

HTH,
--
Rainer Brockerhoff  <rai...@brockerhoff.net>
Belo Horizonte, Brazil
"In the affairs of others even fools are wise
In their own business even sages err."
Weblog: http://www.brockerhoff.net/blog


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please 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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to