5

I have a library that was compiled against Apple's LLVM 4.2 compiler (Base SDK 6.1). In it there is object subscripting.

Imagine that my library has only one class with one method. That method does this:

NSLog(@"****** preTests");
NSDictionary *dictTest = @{ @1 : @1 };
NSLog(@"Initialized Dictionary");
NSArray *arrayTest = @[ @1, @2, @3 ];
NSLog(@"Initialized Array");
NSLog(@"****** arrayTest[1] = %@", arrayTest[1]); // First use of subscripting
NSLog(@"****** dictTest[@1] = %@", dictTest[@1]);

Now I create a new project and link this library in. In my application delegate, I call this method. I compile this application with the GCC LLVM 4.2 compiler. It compiles and links fine.

This application will run without error on iOS 6+. This application will crash on iOS 5 at the "First use of subscripting" (above).

2013-07-03 09:15:51.050 GCCTest[167:707] -[__NSArrayI objectAtIndexedSubscript:]: unrecognized selector sent to instance 0x381fb0

Compile it with the Apple LLVM 4.2 compiler and it will run normally.

objectAtIndexedSubscript: is a method made publicly available in iOS 6 and it is my understanding that it what the syntactic sugar of myArray[0] gets translated to.

Can someone help me understand why I see a crash with GCC and not Apple with iOS 5? I'm guessing it has to do with some macros somewhere... Could this be made not to crash with GCC without editing the code of my library?

Ben Flynn
  • 18,524
  • 20
  • 97
  • 142

1 Answers1

2

According to the "Objective-C Feature Availability Index", NSArray subscripting requires at least LLVM Compiler 4.0.

Starting with iOS 6, NSArray has a objectAtIndexedSubscript: method. For iOS 5, this method is supplied by the static Arclite library that is linked into the application (see e.g. How to enable the new Objective-C object literals on iOS? and the links given in the answer). But that is a Clang only feature, GCC does not support ARC.

So I do not see how you could use array subscripting if the main application is compiled and linked with GCC.

Community
  • 1
  • 1
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • This is a *huge* help. If I force load arclite it runs in GCC. As I understand it, GCC does not need to support ARC to link to ARC code, since anything "ARC" about it is getting transformed during compilation of the library. – Ben Flynn Jul 03 '13 at 18:26
  • Naïve way: Other linker flags: -force_load /Applications/Xcode-4.6.3.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/arc/libarclite_iphoneos.a – Ben Flynn Jul 03 '13 at 18:30
  • @BenFlynn: Glad to hear that. You are right that ARC mainly compiles additional code into the object file (which requires runtime support that is provided by libarclite on iOS 5), but I really have no idea if it is "safe" to link libarclite into a non-ARC application. – Martin R Jul 03 '13 at 18:32