NSArray之后的方括号索引是什么意思? [英] What does a square-bracketed index after an NSArray mean?
问题描述
通过iTunes U开发适用于iPhone和iPad的iOS 7应用程序,以及第120页的Lecture 3幻灯片,有一个测验问题,询问下面的代码行是什么。坦白地说,我有点累了,希望有人能打破它。
Going through iTunes U Developing iOS 7 Apps for iPhone and iPad and in Lecture 3 slides, on page 120, there's a Quiz question that asks what the following line of code does. Frankly, I'm a bit stumped, and hoped someone could break it down.
cardA.contents = @[cardB.contents,cardC.contents][[cardB match:@[cardC]] ? 1 : 0];
所以,我得到第一部分, cardA.contents =
在数组中使用 cardB.contents
和 cardC.contents
的新数组。但是,接下来(我想是)一个索引返回1或0,取决于 cardB
是否匹配包含 cardC
。
So, I get the first part, cardA.contents =
a new array with cardB.contents
and cardC.contents
in the array. But, next comes (I guess??) an index that returns either 1 or 0 depending if cardB
matches an array that includes cardC
.
这里是我不get,也许只是一个语法问题....这是什么?
Here's what I don't "get", and maybe it's just a syntax issue.... is what this does?
如何
cardA.contents = @[cardB.contents,cardC.contents][0];
或
cardA.contents = @[cardB.contents,cardC.contents][1];
有效?
推荐答案
你的理解是完全正确的;你只是缺少一点语法。通过 NSArray 的下标访问> 数组[index]
表单是Clang稍微介绍的集合文字功能的一部分。
Your understanding is completely correct; you're just missing one bit of syntax. Subscripted access of NSArray
s with the array[index]
form is part of the "collection literals" feature introduced by Clang a little while back.
它由编译器转换为 [array objectAtIndexedSubscript:index]
。
这篇关于NSArray之后的方括号索引是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!