3

The documentation says it's available in MacOS 1.08.

So what's the story? What about for iOS5?

It's a very important selector because self[5] will actually turn into [self objectAtIndexedSubscript:5] unless I am missing something.

Doesn't work in NSOrderedSet but works in NSArray.

What am I missing?

user4951
  • 32,206
  • 53
  • 172
  • 282
  • The NSArray docs state that `objectAtIndexedSubscript:` was added in iOS 6 but the subscript syntax works with iOS 5.x. Very strange. – rmaddy Nov 15 '12 at 03:48

4 Answers4

7

While objectAtIndexedSubscript: is not available previous to iOS 6, NSArray and NSDictionarysubscripting is available. That means that you can use syntax like this:

myArray[2] = @"thingie";
myDictionary[@"roger"] = @"barry";

And it will deploy back to iOS 4.

However NSOrderedSet subscripting will not work on iOS 5 and previous. For that, you will need to provide a category that redirects objectAtIndexedSubscript: calls to objectAtIndex:.

Addendum: Apple's docs for NSMutableOrderedSet are also incorrect. It states that index subscripting does an insert, when in reality is does a replace (as one would expect).

big_m
  • 1,408
  • 1
  • 12
  • 24
sobri
  • 1,626
  • 15
  • 28
  • The link says it deploys back to iOS 5 not iOS 4. But great find! – jasongregori Jul 29 '13 at 19:25
  • Yeah, the docs say iOS 5, but ARClite works with iOS 4, so I'm not sure which is right. I haven't tested, and probably few care at this point, but for the sake of historical accuracy, it would be nice to know. – big_m Nov 18 '14 at 03:18
  • Also, [Steven Fisher's answer](http://stackoverflow.com/a/11659848/686385) to a related question describes how to use the subscript syntax while building against the iOS 5 (and maybe iOS 4) SDK. – big_m Nov 18 '14 at 03:23
3

No, only since iOS 6 unfortunately.

Apple has separate documentations for the OS X and the iOS APIs. You have to check the right one: objectAtIndexedSubscript:.

Availability
Available in iOS 6.0 and later.

DrummerB
  • 39,814
  • 12
  • 105
  • 142
  • 1
    Despite the `NSArray` docs stating this, the `someArray[index]` syntax does work with iOS 5.x. But the same syntax crashes at runtime under iOS 5.x using an `NSOrderedSet`. – rmaddy Nov 15 '12 at 03:51
  • 1
    That's because when compiling, `someArray[index]` will actually be transformed into `[someArray objectAtIndex:index]`. This new syntax is a compiler feature and available since Xcode 4.4. Only since iOS 6 however can you override `objectAtIndexedSubscript:`. I'm not sure, but I think `NSOrderedSet` doesn't support the bracket index syntax. – DrummerB Nov 15 '12 at 03:56
  • I understand what the compiler is doing with the new syntax. I just thought it translated the subscript syntax to the `objectAtIndexedSubscript:` method. I added that method to one of my own classes and this allows me to use the subscript syntax with my custom class. – rmaddy Nov 15 '12 at 04:03
  • 1
    DrummerB is incorrect about what's happening. What's actually happening is that the compiler is quietly inserting a category on relevant classes adding -objectAtIndexedSubscript:, if your deployment target is iOS 5. – Catfish_Man Nov 16 '12 at 01:27
  • maddy has it right. And Apple's docs are wrong. The ARCLite library, automatically linked to all projects that have ARC enabled (since Xcode 4.2, I believe), includes the `objectAtIndexedSubscript:`, etc., implementations for arrays and dictionaries. This works at least back to iOS 5 (and probably 4, but I haven't tested or found any definitive references). – big_m Nov 18 '14 at 03:37
  • Just to correct my previous comment, it looks like subscript syntax wasn't supported until LLVM 4.0, in Xcode 4.4/4.5, so that's likely when the ARCLite library picked up the required methods to support subscripting. – big_m Nov 18 '14 at 05:36
1

If you need your code to run on iOS 5, you'll need to replace

myOrderedSetOfHilariousAcronyms[2] = @"ROFL";

with

[myOrderedSetOfHilariousAcronyms setObject:@"ROFL" atIndex:2];
Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
0

I look at the NSOrderedSet.h file and I saw this:

- (id)objectAtIndexedSubscript:(NSUInteger)idx NS_AVAILABLE(10_8, 6_0);

So it doesn't work for IOS5.

user4951
  • 32,206
  • 53
  • 172
  • 282