编译器错误“未找到预期方法”当在NSArray上使用下标时 [英] Compiler error "expected method not found" when using subscript on NSArray
问题描述
我写了这个简单的代码,试用 NSArray
s的新的Objective-C文字语法:
I wrote this simple code to try out the new Objective-C literal syntax for NSArray
s:
NSArray *array = @[@"foo"];
NSLog(@"%@", array[0]);
第一行工作正常,但下标导致错误:
The first line works fine, but the subscripting results in an error:
在类型NSArray *的对象上未找到预期的读取数组元素的方法
Expected method to read array element not found on object of type 'NSArray *'
只是想知道我做错了什么,或者如果文字还没有完全实现。我正在使用Apple LLVM 4.0并使用iOS 5 SDK进行编译。
Just wondering if I have done something wrong, or if the literals haven't been fully implemented yet. I'm compiling with Apple LLVM 4.0 and using the iOS 5 SDK.
这里也是错误的屏幕截图。
Here's a screenshot of the error, too.
推荐答案
您必须使用iOS 6或OS X 10.8 SDK编译 - 否则Foundation对象没有必要的方法用于字面语法的下标位。特别是在这种情况下,下标期望 objectAtIndexedSubscript:
由 NSArray
实现,这是一个创建的与此编译器功能交互的新方法。
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.
进一步阅读一个新的语法,只要与对象创建相关的部分就可以正常工作了 - 我不相信需要任何新的方法。 http://clang.llvm.org/docs/ObjectiveCLiterals.html
Further reading at http://clang.llvm.org/docs/ObjectiveCLiterals.html
*我基于一些由borrrden执行的研究: http://stackoverflow.com/a/11407844/603977
*I base this on a bit of research performed by borrrden: http://stackoverflow.com/a/11407844/603977
我在这个答案上有很多upvote,我真的觉得是建立在borrrden的。请,如果你认为我的答案值得一个upvote,点击并投票在那里。
这篇关于编译器错误“未找到预期方法”当在NSArray上使用下标时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!