iPhone:无法理解以下编码

发布于 2024-11-06 03:41:48 字数 197 浏览 1 评论 0原文

我已经下载了一些示例编码。但我发现了奇怪的编码,或者也许我是第一次看到它。

有人可以帮助我理解以下编码吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寻找我们的幸福 2024-11-13 03:41:48

让我们从内到外把它拆开。

[obj word]

带有选择器 word 的消息,指向指针位于变量 obj 中的对象。

return [obj word];

返回该消息返回的值的语句。

^id (id obj) {
    return [obj word];
}

事情变得有趣了。

这是一个块。我们来拆解一下:

  • ^:这是一个块!
  • id:返回类型。该块返回一个对象指针(id)。
  • (id obj):参数。它需要一个,即在消息表达式中用作接收者的 obj 变量。正如我们从它的名称中可以猜测的那样,正如这里明确指出的那样,它也是一个对象指针。
  • { … }:块的主体。

因此,一个块接受一个对象并返回一个对象。

[response.spellingSuggestions wn_map: ^id (id obj) {
    return [obj word];
}]

该块被传递给 wn_map: 消息。让我们向内追溯,在该消息表达式的另一端:

response.spellingSuggestions

这实际上是另一个消息表达式。它将 spellingSuggestions 消息发送到指针位于 response 变量中的对象。这种表达式称为属性访问表达式,因为 spellingSuggestions 应该是一个正式属性(用 @property 声明),但最终它只是另一个 Objective-C 消息。

或者 response 可能是一个结构体,而这个表达式是一个结构体成员访问,但在现代 Objective-C 代码中,这不太可能。只有您知道,因为您没有显示响应声明。如果其类型为idSomeObjectiveCClass *,则这是一个消息表达式。如果它的类型是其他类型,无论有或没有 struct 关键字,但绝对没有 *,那么它就是一个结构成员访问表达式。

不管怎样,这个表达式的计算结果是一个对象指针,大概是一个以某种方式封装或列出拼写建议的对象。

[response.spellingSuggestions wn_map: ^id (id obj) {
    return [obj word];
}]

...然后您将 wn_map: 消息发送到其中,传递块。我猜想 wn_map: 方法将为每个拼写建议调用该块……

NSArray *wordStrings = [response.spellingSuggestions wn_map: ^id (id obj) {
    return [obj word];
}];

并返回一个指向 NSArray 的指针,您可以用它来初始化 wordStrings 变量。


顺便说一句,另一种写法(假设 wn_map: 做了我认为它做的事情)是:

NSArray *wordStrings = [response valueForKeyPath:@"spellingSuggestions.word"];

或:

NSArray *wordStrings = [response.spellingSuggestions valueForKey:@"word"];

前者将后者作为其工作的一部分(前者也将使用 < code>valueForKey: 来获取 response.spellingSuggestions,而不是像原始代码和后面的修订版那样仅发送 [responsepellingSuggestions])。

假设 spellingSuggestions 返回一个 NSArray,则发送到该数组的 valueForKey: 消息将执行与 wn_map: 和块相同的操作:发送 word 消息给每个建议,并将结果收集到一个新数组中 - 您分配给 wordStrings 的数组。

Let's break it apart, from the inside out.

[obj word]

A message with the selector word to the object whose pointer is in the variable obj.

return [obj word];

A statement returning the value returned by that message.

^id (id obj) {
    return [obj word];
}

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 the obj 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.

[response.spellingSuggestions wn_map: ^id (id obj) {
    return [obj word];
}]

The block is passed to a wn_map: message. Let's drill back inward, at the other end of this message expression:

response.spellingSuggestions

This is actually another message expression. It sends the spellingSuggestions message to the object whose pointer is in the response variable. This sort of expression is called a property access expression, because spellingSuggestions 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 of response. If its type is id or SomeObjectiveCClass *, this is a message expression. If its type is something else, with or without the struct 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.

[response.spellingSuggestions wn_map: ^id (id obj) {
    return [obj word];
}]

… which you then send the wn_map: message to, passing the block. I'd guess that the wn_map: method will call the block for every spelling suggestion…

NSArray *wordStrings = [response.spellingSuggestions wn_map: ^id (id obj) {
    return [obj word];
}];

… 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:

NSArray *wordStrings = [response valueForKeyPath:@"spellingSuggestions.word"];

or:

NSArray *wordStrings = [response.spellingSuggestions valueForKey:@"word"];

The former will do the latter as part of its work (and the former will also use valueForKey: to get response.spellingSuggestions, rather than just sending [response spellingSuggestions] as the original code and the latter revision do).

Assuming spellingSuggestions returns an NSArray, this valueForKey: message to that array will do the same thing as wn_map: and the block: Send the word message to each suggestion, and gather the results into a new array—the one you assign to wordStrings.

诺曦 2024-11-13 03:41:48

这是 Objective-C 的块构造。
从人类的角度来说,这段代码的含义如下:

  1. 获取一个充满对象的数组,
  2. 从它开始构建一个新数组,其中新数组的每个项目都通过建议的匿名方法创建“映射”旧项目(即所有位于 ^ 字符之后)

因此,如果 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:

  1. Take an array filled with objects
  2. Build a new array starting from it, where each item of the new array is creating "mapping" the old item through the proposed anonymous method (i.e. all that is after the ^ character)

So, if the response.spellingSuggestions object is an instance of NSArray filled with objects which respond to the method word, 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文