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: Tue, 21 Aug 2012 22:29:19 -0300
On Aug 20, 2012, at 20:41 , Rainer Brockerhoff <email@hidden> wrote:
> I can confirm NSOrdered[Mutable]Set isn't covered by libarclite. Here's what I'm using for that (easily extended to other classes:
Oops. Forgot to take out my RBXCALL() macros (which just add a trivial test), so below's the corrected version:
@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 = class_getInstanceMethod(class, oldsel);
if (method) {
IMP imp = method_getImplementation(method);
if (imp) {
const char* enc = method_getTypeEncoding(method);
if (enc) {
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:)]) {
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.
Sorry about that.
--
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