编译器错误“找不到预期的方法"在 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.
这是错误的屏幕截图.
推荐答案
您必须使用 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
*我基于 borrrden 进行的一些研究:https://stackoverflow.com/a/11407844/603977
*I base this on a bit of research performed by borrrden: https://stackoverflow.com/a/11407844/603977
我对这个答案获得了很多赞成票,我真的觉得这是建立在 borrrden 的基础上的.请,如果您认为我的回答值得点赞,请点击并在那里投票.
这篇关于编译器错误“找不到预期的方法"在 NSArray 上使用下标时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!