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: Jens Alfke <email@hidden>
- Date: Mon, 20 Aug 2012 14:02:48 -0700
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.
As an experiment I added the category @interface for these methods, but not the @implementation — the app still ran fine (at least in the 5.1 simulator; don't have an iOS 5 device to try it on right now.)
My guess is that the compiler is smart enough to emit calls to e.g. objectForKey: instead of objectForKeyedSubscript: when it sees that the receiver is an instance of NSDictionary. But I haven't yet disassembled the code to verify this.
Here's the declaration I use:
// Compatibility declarations to allow NSArray/NSDictionary subscripting in iOS with Xcode 4.4.
// The compiler needs to see declarations of these methods to make the class subscriptable, but
// the iOS 5.1 SDK doesn't contain them on NSArray/NSDictionary. It appears it isn't necessary
// to actually implement these methods, though...
#if (defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED < 60000)
@interface NSArray (PreiOS6)
- (id)objectAtIndexedSubscript:(NSUInteger)index;
@end
@interface NSMutableArray (PreiOS6)
- (void)setObject: (id)object atIndexedSubscript:(NSUInteger)index;
@end
@interface NSDictionary (PreiOS6)
- (id)objectForKeyedSubscript:(id)key;
@end
@interface NSMutableDictionary (PreiOS6)
- (void)setObject: (id)object forKeyedSubscript:(id)key;
@end
#endif
—Jens
_______________________________________________
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