Skip to content

@jaydee3 /NSContainer+Subscripting.h
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Objective-C Literals: Object Subscripting (LLVM 4.0) - ready to use in iOS 5 & 4!
// 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"]);
}
}
//
// 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
//
// 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
@lhasiuk
- (void)setObject:(id)anObject atIndexedSubscript:(NSUInteger)index {
    [self insertObject:anObject atIndex:index];
}

is incorrect. It should use replaceObjectAtIndex:withObject in cases where the index refers to an existing element.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.