Re: NSArray's objectAtIndex compiles to objectAtIndexedSubscript?
Re: NSArray's objectAtIndex compiles to objectAtIndexedSubscript?
- Subject: Re: NSArray's objectAtIndex compiles to objectAtIndexedSubscript?
- From: Fritz Anderson <email@hidden>
- Date: Fri, 07 Mar 2014 10:10:39 -0600
On 7 Mar 2014, at 6:54 AM, Bill Cheeseman <email@hidden> wrote:
> My code calls -[NSArray objectAtIndex:]. I compile it with Xcode 5.0.2 on OS X 10.9.x Mavericks in a project with the target's Base SDK set to 10.9 and the OS X Deployment Target set to OS X 10.7. It works fine when I run it on OS X 10.9 or OS X 10.8.
>
> However, when I run it on Mac OS X 10.7 Lion, I get a runtime error claiming it encountered an invalid argument, namely, the unrecognized selector -objectAtIndexedSubscript:. The NSArray Class Reference notes that -objectAtIndexedSubscript: is available only in OS X 10.8 and later, and that it is "identical to objectAtIndex:". Of course, -objectAtIndex: still exists in the 10.9 API, and it is not marked as deprecated.
>
> It appears, therefore, that the compiler generated a call to -objectAtIndexedSubscript: even though my source specifies -objectAtIndex: and a deployment version of 10.7.
I tried a Foundation command-line tool, 10.9 SDK, 10.7 deployment:
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSArray * myArray = @[ @"alpha",
@"beta",
@"gamma",
@"delta"
];
NSString * zerothString = [myArray objectAtIndex: 0];
NSLog(@"The string is %@", zerothString);
}
return 0;
}
Note that I’m using an array literal, which is backwards-compatible, and you’d think that might tempt the compiler to emit objectAtIndexedSubscript:.
My disassembly of main.o (Hopper Disassembler, UI a bit odd, but very valuable) shows no reference to objectAtIndexedSubscript:, so it’s not the compiler.
There is a reference to _objc_msgSend_fixup, which you’d expect to be generic. The runtime _might_ special-case objectAtIndex: as an alias for objectAtIndexedSubscript:, but that’s a runtime issue, not compile-time. I saw nothing in the public source of the runtime, but I didn’t put much effort into it.
And wasn’t the indexed-subscript notation supported in ObjC in late 10.7.x? And I seem to remember that objectAtIndexedSubscript: was implemented then (and retroactively supported as “public” API in 10.7 MAS submissions). I may be wrong about the chronology.
See the Assembly assistant while your code is in the main editor.
This is all bloviation on what Jens said: Check the backtrace.
— F
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden