Re: How to make Obj-C collection subscripting work on iOS 5?
Re: How to make Obj-C collection subscripting work on iOS 5?
- Subject: Re: How to make Obj-C collection subscripting work on iOS 5?
- From: Rainer Brockerhoff <email@hidden>
- Date: Mon, 20 Aug 2012 20:41:47 -0300
On Aug 20, 2012, at 19:41 , email@hidden wrote:
> Date: Mon, 20 Aug 2012 15:16:43 -0700
> From: Greg Parker <email@hidden>
> Message-ID: <email@hidden>
>
> On Aug 20, 2012, at 2:02 PM, Jens Alfke <email@hidden> 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 <email@hidden>
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 (email@hidden)
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:
This email sent to email@hidden