Compiler error “expected method not found” when using subscript on NSArray

后端 未结 7 1889
悲哀的现实
悲哀的现实 2020-11-29 04:45

I wrote this simple code to try out the new Objective-C literal syntax for NSArrays:

NSArray *array = @[@"foo"];
NSLog(@"%@",         


        
相关标签:
7条回答
  • 2020-11-29 05:18

    You've got to be compiling with the iOS 6 or OS X 10.8 SDKs -- otherwise Foundation objects don't have the necessary methods for the subscripting bit of the literal syntax.* Specifically in this case, the subscripting expects objectAtIndexedSubscript: to be implemented by NSArray, and that's a new method that was created to interact with this compiler feature. The parts of the new syntax that just have to do with object creation should work fine, though -- I don't believe that requires any new methods.

    Further reading at http://clang.llvm.org/docs/ObjectiveCLiterals.html


    *I base this on a bit of research performed by borrrden: https://stackoverflow.com/a/11407844/603977

    I've gotten a lot of upvotes on this answer, which I really feel is founded on borrrden's. Please, if you think my answer is worth an upvote, click through and vote there too.

    0 讨论(0)
提交回复
热议问题