diff --git a/Headers/Foundation/NSArray.h b/Headers/Foundation/NSArray.h index 42e6dcfce..0b97fce53 100644 --- a/Headers/Foundation/NSArray.h +++ b/Headers/Foundation/NSArray.h @@ -213,6 +213,11 @@ DEFINE_BLOCK_TYPE(GSPredicateBlock, BOOL, id, NSUInteger, BOOL*); options: (NSEnumerationOptions)opts passingTest: (GSPredicateBlock)predicate; #endif +/** + * Accessor for subscripting. This is called by the compiler when you write + * code like anArray[12]. It should not be called directly. + */ +- (id) objectAtIndexedSubscript: (size_t)anIndex; @end @@ -264,7 +269,10 @@ DEFINE_BLOCK_TYPE(GSPredicateBlock, BOOL, id, NSUInteger, BOOL*); #if OS_API_VERSION(GS_API_MACOSX, GS_API_LATEST) - (void) setValue: (id)value forKey: (NSString*)key; #endif - +/** + * Set method called by the compiler with array subscripting. + */ +- (void) setObject: (id)anObject atIndexedSubscript: (size_t)anIndex; @end #if defined(__cplusplus) diff --git a/Headers/Foundation/NSDictionary.h b/Headers/Foundation/NSDictionary.h index c38a2c92f..8a00dd3da 100644 --- a/Headers/Foundation/NSDictionary.h +++ b/Headers/Foundation/NSDictionary.h @@ -108,8 +108,10 @@ DEFINE_BLOCK_TYPE(GSKeysAndObjectsPredicateBlock, BOOL, id, id, BOOL*); #if OS_API_VERSION(GS_API_MACOSX, GS_API_LATEST) - (BOOL) writeToURL: (NSURL*)url atomically: (BOOL)useAuxiliaryFile; #endif - - +/** + * Method called by array subscripting. + */ +- (id) objectForKeyedSubscript: (id)aKey; @end @interface NSMutableDictionary: NSDictionary @@ -128,6 +130,11 @@ DEFINE_BLOCK_TYPE(GSKeysAndObjectsPredicateBlock, BOOL, id, id, BOOL*); - (void) takeStoredValue: (id)value forKey: (NSString*)key; - (void) takeValue: (id)value forKey: (NSString*)key; #endif +/** + * Method called by array subscripting. + */ +- (void) setObject: (id)anObject forKeyedSubscript: (id)aKey; + @end #if defined(__cplusplus) diff --git a/Headers/GNUstepBase/GSVersionMacros.h b/Headers/GNUstepBase/GSVersionMacros.h index 16d37e031..ea6cc4024 100644 --- a/Headers/GNUstepBase/GSVersionMacros.h +++ b/Headers/GNUstepBase/GSVersionMacros.h @@ -270,7 +270,7 @@ #if defined(__clang__) && defined(__OBJC__) static inline void gs_consumed(id NS_CONSUMED o) __attribute__ ((unused)); -static inline void gs_consumed(id NS_CONSUMED o) { return; } +static inline void gs_consumed(id NS_CONSUMED __attribute__ ((unused))o) { return; } #define GS_CONSUMED(O) gs_consumed(O); #else #define GS_CONSUMED(O) diff --git a/Source/NSArray.m b/Source/NSArray.m index 7f30c94b4..a917d4b4f 100644 --- a/Source/NSArray.m +++ b/Source/NSArray.m @@ -920,6 +920,11 @@ static SEL rlSel; return nil; } +- (id) objectAtIndexedSubscript: (size_t)index +{ + return [self objectAtIndex: (NSUInteger)index]; +} + - (NSArray *) objectsAtIndexes: (NSIndexSet *)indexes { //FIXME: probably slow! @@ -1921,6 +1926,11 @@ compare(id elem1, id elem2, void* context) [self subclassResponsibility: _cmd]; } +- (void)setObject: (id)anObject atIndexedSubscript: (size_t)anIndex +{ + [self replaceObjectAtIndex: (NSUInteger)anIndex withObject: anObject]; +} + /** Replaces the values in the receiver at the locations given by the * indexes set with values from the objects array. */ diff --git a/Source/NSDictionary.m b/Source/NSDictionary.m index 817af710b..582a336f1 100644 --- a/Source/NSDictionary.m +++ b/Source/NSDictionary.m @@ -252,6 +252,11 @@ static SEL appSel; return 0; } +- (id) objectForKeyedSubscript: (id)aKey +{ + return [self objectForKey: aKey]; +} + /** * Return an enumerator object containing all the objects of the dictionary. */ @@ -1321,6 +1326,11 @@ compareIt(id o1, id o2, void* context) [self subclassResponsibility: _cmd]; } +- (void) setObject: (id)anObject forKeyedSubscript: (id)aKey +{ + [self setObject: anObject forKey: aKey]; +} + /** * Remove key-value mapping for given key aKey. No error if there is no * mapping for the key. A warning will be generated if aKey is nil. diff --git a/Tests/base/NSArray/basic.m b/Tests/base/NSArray/basic.m index 9ec83722b..fc0f56e16 100644 --- a/Tests/base/NSArray/basic.m +++ b/Tests/base/NSArray/basic.m @@ -34,6 +34,17 @@ int main() #endif obj = [obj objectAtIndex: 0]; PASS([obj isKindOfClass: [NSMutableArray class]] == YES,"array mutable"); + START_SET("NSArray subscripting") +# ifndef __has_feature +# define __has_feature(x) 0 +# endif +#if __has_feature(objc_subscripting) + NSArray *a = @[ @"foo", @"bar" ]; + PASS([@"foo" isEqualToString:a[0]], "Array subscripting works"); +# else + SKIP("No collection subscripting support in the compiler.") +# endif + END_SET("NSArray subscripting") [arp release]; arp = nil; return 0; } diff --git a/Tests/base/NSDictionary/basic.m b/Tests/base/NSDictionary/basic.m index 0da4d8af2..a0477bf9b 100644 --- a/Tests/base/NSDictionary/basic.m +++ b/Tests/base/NSDictionary/basic.m @@ -2,6 +2,7 @@ #import "ObjectTesting.h" #import #import +#import #import int main() @@ -27,6 +28,19 @@ int main() test_NSCoding(testObjs); test_NSCopying(@"NSDictionary", @"NSMutableDictionary", testObjs, YES, NO); test_NSMutableCopying(@"NSDictionary", @"NSMutableDictionary", testObjs); + START_SET("NSArray subscripting") +# ifndef __has_feature +# define __has_feature(x) 0 +# endif +#if __has_feature(objc_subscripting) + NSDictionary *dictionary = @{@123 : @123.4 , + @"date" : @"today" }; + PASS([dictionary[@123] isEqual: @123.4], "Dictionary subscripting works"); +# else + SKIP("No dictionary subscripting support in the compiler.") +# endif + END_SET("NSDictionary subscripting") + [arp release]; arp = nil; return 0;