iPhone:无法理解以下编码
我已经下载了一些示例编码。但我发现了奇怪的编码,或者也许我是第一次看到它。
有人可以帮助我理解以下编码吗?
NSArray *wordStrings = [response.spellingSuggestions wn_map: ^id (id obj) {
return [obj word];
}];
I have downloaded some example coding. But I found strange coding or maybe I have seen it first time.
Can anybody help me to understand following coding?
NSArray *wordStrings = [response.spellingSuggestions wn_map: ^id (id obj) {
return [obj word];
}];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
让我们从内到外把它拆开。
带有选择器
word
的消息,指向指针位于变量obj
中的对象。返回该消息返回的值的语句。
事情变得有趣了。
这是一个块。我们来拆解一下:
^
:这是一个块!id
:返回类型。该块返回一个对象指针(id
)。(id obj)
:参数。它需要一个,即在消息表达式中用作接收者的 obj 变量。正如我们从它的名称中可以猜测的那样,正如这里明确指出的那样,它也是一个对象指针。{ … }
:块的主体。因此,一个块接受一个对象并返回一个对象。
该块被传递给
wn_map:
消息。让我们向内追溯,在该消息表达式的另一端:这实际上是另一个消息表达式。它将
spellingSuggestions
消息发送到指针位于response
变量中的对象。这种表达式称为属性访问表达式,因为spellingSuggestions
应该是一个正式属性(用@property
声明),但最终它只是另一个 Objective-C 消息。或者
response
可能是一个结构体,而这个表达式是一个结构体成员访问,但在现代 Objective-C 代码中,这不太可能。只有您知道,因为您没有显示响应
声明。如果其类型为id
或SomeObjectiveCClass *
,则这是一个消息表达式。如果它的类型是其他类型,无论有或没有 struct 关键字,但绝对没有*
,那么它就是一个结构成员访问表达式。不管怎样,这个表达式的计算结果是一个对象指针,大概是一个以某种方式封装或列出拼写建议的对象。
...然后您将
wn_map:
消息发送到其中,传递块。我猜想wn_map:
方法将为每个拼写建议调用该块……并返回一个指向 NSArray 的指针,您可以用它来初始化
wordStrings
变量。顺便说一句,另一种写法(假设
wn_map:
做了我认为它做的事情)是:或:
前者将后者作为其工作的一部分(前者也将使用 < code>valueForKey: 来获取
response.spellingSuggestions
,而不是像原始代码和后面的修订版那样仅发送[responsepellingSuggestions]
)。假设
spellingSuggestions
返回一个 NSArray,则发送到该数组的valueForKey:
消息将执行与wn_map:
和块相同的操作:发送word
消息给每个建议,并将结果收集到一个新数组中 - 您分配给wordStrings
的数组。Let's break it apart, from the inside out.
A message with the selector
word
to the object whose pointer is in the variableobj
.A statement returning the value returned by that message.
Here things get interesting.
This is a block. Let's disassemble it:
^
: It's a block!id
: Return type. This block returns an object pointer (id
).(id obj)
: Arguments. It takes one, which is theobj
variable used as the receiver in the message expression. As we can guess from its name and as is made explicit here, it is also an object pointer.{ … }
: The body of the block.So, a block taking one object and returning an object.
The block is passed to a
wn_map:
message. Let's drill back inward, at the other end of this message expression:This is actually another message expression. It sends the
spellingSuggestions
message to the object whose pointer is in theresponse
variable. This sort of expression is called a property access expression, becausespellingSuggestions
should be a formal property (declared with@property
), but ultimately it's just another Objective-C message.Or
response
could be a structure and this expression a structure member access, but in modern Objective-C code, that's unlikely. Only you know, since you didn't show the declaration ofresponse
. If its type isid
orSomeObjectiveCClass *
, this is a message expression. If its type is something else, with or without thestruct
keyword but definitely without an*
, then it is a structure member access expression.Either way, this expression evaluates to an object pointer, presumably an object that somehow encapsulates or lists spelling suggestions.
… which you then send the
wn_map:
message to, passing the block. I'd guess that thewn_map:
method will call the block for every spelling suggestion…… and return a pointer to an NSArray, which you initialize the
wordStrings
variable with.Incidentally, another way to write this (assuming
wn_map:
does what I think it does) would have been:or:
The former will do the latter as part of its work (and the former will also use
valueForKey:
to getresponse.spellingSuggestions
, rather than just sending[response spellingSuggestions]
as the original code and the latter revision do).Assuming
spellingSuggestions
returns an NSArray, thisvalueForKey:
message to that array will do the same thing aswn_map:
and the block: Send theword
message to each suggestion, and gather the results into a new array—the one you assign towordStrings
.这是 Objective-C 的块构造。
从人类的角度来说,这段代码的含义如下:
^
字符之后)因此,如果
response.spellingSuggestions
对象是NSArray
的实例,其中填充了响应方法 < code>word,您将获得一个新数组,其中每个对象都是[obj word]
调用的结果。例如,您可以使用这种方法将对象列表转换为相应字符串描述的列表。更新
您可以在 Apple 开发者参考页面,如 @Steven 在他的回答中所述。
希望这有帮助
This is the blocks construct for Objective-C.
What this code means, in human terms, is the following:
^
character)So, if the
response.spellingSuggestions
object is an instance ofNSArray
filled with objects which respond to the methodword
, you will obtain a new array where each object is the result of the[obj word]
call. E.g. you can use this approach to convert a list of objects in a list of the corresponding string description.Update
You can find more information about the block construct at the Apple Developer reference page, as stated by @Steven in his answer.
Hope this helps
这就是“块”结构。例如,请参见 http://developer。 apple.com/library/ios/#documentation/general/conceptual/DevPedia-CocoaCore/Block.html
This is the "blocks" construct. See, e.g., http://developer.apple.com/library/ios/#documentation/general/conceptual/DevPedia-CocoaCore/Block.html