Created
August 10, 2012 14:35
-
-
Save calimarkus/3314686 to your computer and use it in GitHub Desktop.
Objective-C Literals: Object Subscripting (LLVM 4.0) - ready to use in iOS 5 & 4!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// example | |
int main(int argc, char *argv[]) | |
{ | |
@autoreleasepool { | |
NSMutableArray *array = [NSMutableArray arrayWithObject: @"vorher"]; | |
NSLog(@"%@", array[0]); | |
array[0] = @"haha"; | |
NSLog(@"%@", array[0]); | |
NSMutableDictionary *dict = [NSMutableDictionary dictionary]; | |
dict[@"test"] = @"jap"; | |
NSLog(@"%@", dict[@"test"]); | |
dict[@"test"] = @"change"; | |
NSLog(@"%@", dict[@"test"]); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// NSContainer+Subscripting.h | |
// | |
// Created by Markus Emrich on 10.08.12. | |
// Copyright 2012 nxtbgthng. All rights reserved. | |
// | |
#if !defined(__IPHONE_6_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0 | |
#import "NSContainer+Subscripting.h" | |
@interface NSArray (Subscripting) | |
- (id)objectAtIndexedSubscript:(NSInteger)index; | |
@end | |
@interface NSMutableArray (MutableSubscripting) | |
- (void)setObject:(id)anObject atIndexedSubscript:(NSUInteger)index; | |
@end | |
@interface NSDictionary (Subscripting) | |
- (id)objectForKeyedSubscript:(id)key; | |
@end | |
@interface NSMutableDictionary (MutableSubscripting) | |
- (void)setObject:(id)object forKeyedSubscript:(id)key; | |
@end | |
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// NSContainer+Subscripting.m | |
// | |
// Created by Markus Emrich on 10.08.12. | |
// Copyright 2012 nxtbgthng. All rights reserved. | |
// | |
#if !defined(__IPHONE_6_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0 | |
#import "NSContainer+Subscripting.h" | |
@implementation NSArray (Subscripting) | |
- (id)objectAtIndexedSubscript:(NSInteger)index { | |
return [self objectAtIndex:index]; | |
} | |
@end | |
@implementation NSMutableArray (MutableSubscripting) | |
- (void)setObject:(id)anObject atIndexedSubscript:(NSUInteger)index { | |
[self insertObject:anObject atIndex:index]; | |
} | |
@end | |
@implementation NSDictionary (Subscripting) | |
-(id)objectForKeyedSubscript:(id)key { | |
return [self objectForKey: key]; | |
} | |
@end | |
@implementation NSMutableDictionary (MutableSubscripting) | |
- (void)setObject:(id)object forKeyedSubscript:(id)key { | |
[self setObject:object forKey: key]; | |
} | |
@end | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
is incorrect. It should use replaceObjectAtIndex:withObject in cases where the index refers to an existing element.