Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

翻译 Objective-C Direct Methods (#290) #299

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

mobilefellow
Copy link
Member

@mobilefellow mobilefellow commented Mar 18, 2020

这篇文章对应的 Issue 链接是:#290

本文的大致阅读时长:[7] 分钟

PR Checklist:

请校对同学帮忙校对。

Copy link
Collaborator

@Nemocdz Nemocdz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一次校对完毕


<!--此处开始正文-->

现在我们很少为 Objective-C 的新特性而兴奋。因为任何此类改进都是为了增强与 Swift 的互操作性,而不是对语言本身的投资。_(见 [nullability](https://developer.apple.com/swift/blog/?id=25)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

现在我们很少为 Objective-C 的新特性而兴奋 -> 现在我们很少因 Objective-C 的新特性而感到兴奋

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的


<!--此处开始正文-->

现在我们很少为 Objective-C 的新特性而兴奋。因为任何此类改进都是为了增强与 Swift 的互操作性,而不是对语言本身的投资。_(见 [nullability](https://developer.apple.com/swift/blog/?id=25)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

任何和都有点重复。因为任何此类 -> 因为这类改进

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的


因此,[最近合并到 Clang 的补丁](https://reviews.llvm.org/D69991) 令人十分惊讶,它为 Objective-C 方法增加了一种新的直接派发(Direct Dispatch)机制。

这种新特征的起源尚不清楚,我们最多只能找到苹果内部的一个 [Radar 号](/bug-reporting/)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个新特性

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的


如果你觉得所有这些间接操作似乎做了很多事情……在某种程度上,你是对的!

如果你的代码中有一个热路径(被频繁调用的昂贵方法),你能想象避免这些间接方法所带来的好处。为此,一些开发人员使用 C 函数作为绕过动态派发的一种方法。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你能想象避免这些间接方法所带来的好处 -> 可以想象如果避免了这些间接方法所带来的收益

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

前半句已经有个“如果”了,后半句再来个有点绕口。要不改为“可以想象避免这些间接方法所带来的收益”

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯,可以


---

你可以想象,从现在到明年的开发者大会这段时间,工程师们会遍历每个 SDK framework,使用 `objc_direct` 注释私有方法,使用 `objc_direct_members` 注释私有类,这是一种比较轻便的逐步收紧 SDK 的方法。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这是一种比较轻便的逐步收紧 SDK 的方法 -> 这是一种比较轻量的可以逐步减小 SDK 的方法

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的


你可以想象,从现在到明年的开发者大会这段时间,工程师们会遍历每个 SDK framework,使用 `objc_direct` 注释私有方法,使用 `objc_direct_members` 注释私有类,这是一种比较轻便的逐步收紧 SDK 的方法。

如果这是真的,也许我们应该开始怀疑 Objective-C 的所有新特性了。因为它们不是为 Swift 服务,就是为苹果服务。尽管 Objective-C 在编程史和苹果公司的发展史上占有重要的地位,但也不得不将它视为 _历史_。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

也许我们应该开始怀疑 Objective-C 的所有新特性了 -> 也许我们应该怀疑是否有真正的 Objective-C 新特性了

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的


你可以想象,从现在到明年的开发者大会这段时间,工程师们会遍历每个 SDK framework,使用 `objc_direct` 注释私有方法,使用 `objc_direct_members` 注释私有类,这是一种比较轻便的逐步收紧 SDK 的方法。

如果这是真的,也许我们应该开始怀疑 Objective-C 的所有新特性了。因为它们不是为 Swift 服务,就是为苹果服务。尽管 Objective-C 在编程史和苹果公司的发展史上占有重要的地位,但也不得不将它视为 _历史_。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

因为它们 -> 因为目前它们

Copy link
Member Author

@mobilefellow mobilefellow Apr 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我改用“因为现在它们”吧,感觉更顺口一些

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以的


你可以想象,从现在到明年的开发者大会这段时间,工程师们会遍历每个 SDK framework,使用 `objc_direct` 注释私有方法,使用 `objc_direct_members` 注释私有类,这是一种比较轻便的逐步收紧 SDK 的方法。

如果这是真的,也许我们应该开始怀疑 Objective-C 的所有新特性了。因为它们不是为 Swift 服务,就是为苹果服务。尽管 Objective-C 在编程史和苹果公司的发展史上占有重要的地位,但也不得不将它视为 _历史_。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

但也不得不将它视为 历史 -> 但也不得不承认它已经是历史了

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的

因此,[最近合并到 Clang 的补丁](https://reviews.llvm.org/D69991) 令人十分惊讶,它为 Objective-C 方法增加了一种新的直接派发(Direct Dispatch)机制。

这种新特征的起源尚不清楚,我们最多只能找到苹果内部的一个 [Radar 号](/bug-reporting/)
([`2684889`](rdar://2684889))。不过除了用来估算它的相对年龄(据我们估计,大约在20世纪初),这个 Radar 号什么用也没有。幸运的是,[这个特性](https://github.com/llvm/llvm-project/commit/d4e1ba3fa9dfec2613bdcc7db0b58dea490c56b1) 提供了足够的文档和测试覆盖,可以很好地了解它是如何工作的。(向它的实现者 Pierre Habouzit、评审经理 John McCall 和其他 LLVM 贡献者致敬)。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

20 世纪,格式不对

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的

Copy link
Collaborator

@Nemocdz Nemocdz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

还有一些需要修改的地方

这种新特性的起源尚不清楚,我们最多只能找到苹果内部的一个 [Radar 号](/bug-reporting/)
([`2684889`](rdar://2684889))。不过除了用来估算它的相对年龄(据我们估计,大约在 20 世纪初),这个 Radar 号什么用也没有。幸运的是,[这个特性](https://github.com/llvm/llvm-project/commit/d4e1ba3fa9dfec2613bdcc7db0b58dea490c56b1) 提供了足够的文档和测试覆盖,可以很好地了解它是如何工作的。(向它的实现者 Pierre Habouzit、评审经理 John McCall 和其他 LLVM 贡献者致敬)。

在这周的 NSHipster 上,我们将借此机会回顾 Objective-C 的方法调度机制,并试图理解语言的这个新特性对未来代码库的潜在影响。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

并试图理解语言的这个新特性 -> 并试图理解这个新的语言特性

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok


1980 年代 Brad Cox 和 Tom Love 开始开发 Objective-C 的第一个版本,寻求在坚实的 C 语言基础上实现 Smalltalk 的面向对象模式。经过 90 年代一系列的偶然事件,这门语言成为了 NeXT,以及接下来的 Apple 的官方语言。

对于我们这些在 iPhone 时代开始学习 Objective-C 的人来说,这门语言常常被视为苹果的又一项专利技术 —— 它是该公司 “[非我所创(Not invented here,NIH)](https://zh.wikipedia.org/wiki/非我所創)” 文化下众多晦涩的副产品之一。然而,Objective-C 不仅仅是“一个面向对象的 C 语言”,它更是一门 _为了面向对象而生的_ 语言,和其他任何语言一样,都有很强的面向对象特征。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为了面向对象而生的 两边没有空格

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

必须有空格才行,不然会展示为下划线,看下面两个例子。

没空格: 一门_为了面向对象而生的_语言
有空格: 一门 为了面向对象而生的 语言

Copy link
Collaborator

@Nemocdz Nemocdz Apr 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感觉这个因为 Github 的评论不是完全标准的 markdown 格式而已,不然你使用*代替_,斜体,两边有空格是不对的

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我在翻译中也试过删除空格,还是会有展示问题,见下面的链接。我们的翻译指南中也没说斜体前后不能有空格,只要在全文中保持了一致的样式,也没有问题。

mobilefellow@faa5e53

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

试下用 * 呢?我看网站其余文章都是使用 * 的,且没有保留空格,建议保持一致


1980 年代 Brad Cox 和 Tom Love 开始开发 Objective-C 的第一个版本,寻求在坚实的 C 语言基础上实现 Smalltalk 的面向对象模式。经过 90 年代一系列的偶然事件,这门语言成为了 NeXT,以及接下来的 Apple 的官方语言。

对于我们这些在 iPhone 时代开始学习 Objective-C 的人来说,这门语言常常被视为苹果的又一项专利技术 —— 它是该公司 “[非我所创(Not invented here,NIH)](https://zh.wikipedia.org/wiki/非我所創)” 文化下众多晦涩的副产品之一。然而,Objective-C 不仅仅是“一个面向对象的 C 语言”,它更是一门 _为了面向对象而生的_ 语言,和其他任何语言一样,都有很强的面向对象特征。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

和其他任何语言一样,都有很强的面向对象特征。不太通顺,这里的意思是跟其他面向对象语言一样,都有很着很浓的面向对象的特征

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok


1980 年代 Brad Cox 和 Tom Love 开始开发 Objective-C 的第一个版本,寻求在坚实的 C 语言基础上实现 Smalltalk 的面向对象模式。经过 90 年代一系列的偶然事件,这门语言成为了 NeXT,以及接下来的 Apple 的官方语言。

对于我们这些在 iPhone 时代开始学习 Objective-C 的人来说,这门语言常常被视为苹果的又一项专利技术 —— 它是该公司 “[非我所创(Not invented here,NIH)](https://zh.wikipedia.org/wiki/非我所創)” 文化下众多晦涩的副产品之一。然而,Objective-C 不仅仅是“一个面向对象的 C 语言”,它更是一门 _为了面向对象而生的_ 语言,和其他任何语言一样,都有很强的面向对象特征。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

斜体两边不需要空格,全文检查一遍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上,需要空格


在 Objective-C 中,每个类(`class`)都维护着一个 dispatch table(派发表),用于在运行时处理消息。dispatch table 的每一项都是一个方法(`Method`),它关联着一个 selector(`SEL`),和对应的实现(`IMP`),即一个 C 函数的指针。对象收到消息后,查询它所在类的 dispatch table。如果能找到该 selector 的实现,那么对应的函数将被调用。否则,对象将查询父类的 dispatch table。这个过程会沿着继承链一直向上,直到找到匹配项,或者由根类(`NSObject`)决定无法识别该 selector。

> 这里面还不包括 Objective-C 如何让你替换方法实现以及在运行时动态创建新类。你所能做的绝对是疯狂的。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你所能做的绝对是疯狂的 -> 你能做不少疯狂的事情

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

@end
```

> 根据我们的统计,添加 `direct` 后 `@property` 的属性总数达到 16 个:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

添加 direct 后,@Property... 加个逗号分开好一点

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

@end
```

> 根据我们的统计,添加 `direct` 后 `@property` 的属性总数达到 16 个:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

属性修饰词总数

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

@end
```

> 动态方法不能被子类的直接方法 override,直接方法更是完全不能被 override。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

更是 -> 则

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok


---

对于我们这些关注性能的开发人员来说,直接方法似乎是一个非常有用的特性。但这里有个转折:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

但这里有个转折 -> 但事实上

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK


iPhone 硬件已经不再是以前那个资源有限的平台。因此,除非苹果正在准备一个新的嵌入式平台([AR眼镜,有人知道吗?](http://appleinsider.com/articles/17/01/09/rumor-apple-working-with-carl-zeiss-on-ar-glasses-to-debut-in-2018)),我们觉得苹果在 2019 年实现 Objective-C 直接方法的最合理解释,除了性能之外,肯定还有其他原因。

> [Mike Ash](https://www.mikeash.com) 是互联网上最著名的 `objc_msgSend` 专家。多年以来在理解 Objective-C 运行时的方面,他的文章是苹果官方以外最深刻、最完整的。对于那些好奇的人来说,[“剖析 ARM64 上的 objc_msgSend”](https://www.mikeash.com/pyblog/friday-qa-2017-06-30-dissecting-objc_msgsend-on-arm64.html) 是一个很好的开始。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是一个很好的开始 -> 是一个很好的入门读物

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants