NSArray[] vs NSMutableArray[] in iOS10 and iOS11(beta)

we found iOS gets element from NSMutableArray with “[index]”, that has different implementation between iOS10 and iOS11(beta).

NSMutableArray[] : iOS10(objectAtIndex:), iOS11(objectAtIndexedSubscript:)

code:

NSMutableArray *testArray = [NSMutableArray array]; NSLog(testArray[0]);

in iOS 10:

2017–06–19 08:58:17.718 TestApp[24113:355722] *** Terminating app due to uncaught exception ‘NSRangeException’, reason: ‘*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array’
*** First throw call stack:
(…
2 CoreFoundation 0x0000000106a5c05b -[__NSArrayM objectAtIndex:] + 203

in iOS 11:

2017–06–19 09:03:05.142104+0800 TestApp[24349:359934] *** Terminating app due to uncaught exception ‘NSRangeException’, reason: ‘*** _deque_objectAtIndex: index 0 beyond bounds for empty array’
*** First throw call stack:
(…
3 CoreFoundation 0x000000010f33ca64 -[__NSArrayM objectAtIndexedSubscript:] + 148
you can see using “[]” to get element from NSMutableArray, it will call different methods between ios 10(objectAtIndex:) and ios 11(objectAtIndexedSubscript:).

How about NSArray?

NSArray[] : iOS10(objectAtIndex:), iOS11(objectAtIndex:)

NSArray *testArray = [[NSArray alloc] init];
NSLog(testArray[0]);

in iOS 10

2017–06–19 08:56:08.606 TestApp[23892:352223] *** Terminating app due to uncaught exception ‘NSRangeException’, reason: ‘*** -[__NSArray0 objectAtIndex:]: index 0 beyond bounds for empty NSArray’
*** First throw call stack:
(…
2 CoreFoundation 0x000000010df8227d -[__NSArray0 objectAtIndex:] + 93

in iOS 11

2017–06–19 09:04:08.902340+0800 TestApp[24406:361500] *** Terminating app due to uncaught exception ‘NSRangeException’, reason: ‘*** -[__NSArray0 objectAtIndex:]: index 0 beyond bounds for empty NSArray’
*** First throw call stack:
(…
2 CoreFoundation 0x000000011326799d -[__NSArray0 objectAtIndex:] + 93
you can see using “[]” to get element from NSArray it will call same methods (objectAtIndex:)between ios 10 and ios 11.

Conclusion

If you want to swizzle NSMutableArray [], in iOS 11, you need swizzle objectAtIndexedSubscript:

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.