Re:Xcode 4.4 adds support for Obj-C literals
by upinsmoke on Saturday August 18, 2012 @06:54AM
(#4400795)
Subscripting only on OS X not iOS.
[ Parent
| on HN ]
Re:Xcode 4.4 adds support for Obj-C literals
by weiran on Saturday August 18, 2012 @07:06AM
(#4400817)
You can use them on iOS 5.1 by implementing the methods the subscripts map to on NSArray and NSDictionary (as well as mutable) in categories.
For NSArray:
- (id)objectAtIndexedSubscript:(NSUInteger)index {
return [self objectAtIndex:index];
}
NSMutableArray: - (void)setObject:(id)object atIndexedSubscript:(NSUInteger)index {
if (index < self.count){
if (object)
[self replaceObjectAtIndex:index withObject:object];
else
[self removeObjectAtIndex:index];
} else {
[self addObject:object];
}
}
NSDictionary: - (id)objectForKeyedSubscript:(id)key {
return [self objectForKey:key];
}
NSMutableDictionary: - (void)setObject:(id)object forKeyedSubscript:(id)key {
[self setObject:object forKey:key];
}
Re:Xcode 4.4 adds support for Obj-C literals
by puls on Saturday August 18, 2012 @12:59PM
(#4401595)
The implementation already exists in iOS 5, you just need to make the compiler happy; a header that declares them is sufficient.
Re:Xcode 4.4 adds support for Obj-C literals
by michaelmior on Saturday August 18, 2012 @08:33AM
(#4400968)
Is it just as easy as creating categories that add these methods?
Re:Xcode 4.4 adds support for Obj-C literals
by lazerwalker on Saturday August 18, 2012 @11:01AM
(#4401293)
The functionality is present for iOS, it just isn't being exposed. If you create a category header file (just header, not implementation) the compiler'll pick up on it and all will work fine.
This isn't my code, but I've been happily using it for a week or so now: https://gist.github.com/3299750
Re:Xcode 4.4 adds support for Obj-C literals
by BenjieGillam on Saturday August 18, 2012 @07:47AM
(#4400896)
Apparently OSX 10.8 and iOS 6 bring support for subscripting to native NSArray/NSDictionary, but I think you can add subscripts to your own classes already just by implementing the following methods:
-(id)objectAtIndexedSubscript:(NSUInteger)index;
-(void)setObject:(id)object atIndexedSubscript:(NSUInteger)index;
as weiran has shown for NS(Mutable)Array/NS(Mutable)Dictionary.