-
Notifications
You must be signed in to change notification settings - Fork 5k
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
对元素持有弱引用的Swift数组 #1692
对元素持有弱引用的Swift数组 #1692
Conversation
@sqrthree 认领校对 |
@sqrthree 申请校对 之前一直比较忙 |
@@ -1,26 +1,26 @@ | |||
> * 原文地址:[Swift Arrays Holding Elements With Weak References](https://marcosantadev.com/swift-arrays-holding-elements-weak-references/) | |||
> * 原文作者:[Marco Santarossa](https://marcosantadev.com/about-me/) | |||
> * 译文出自:[掘金翻译计划](https://github.com/xitu/gold-miner) | |||
> * 译者: | |||
> * 译者:[zhangqippp](https://github.com/zhangqippp) | |||
> * 校对者: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
||
For “not type-safe”, I mean that the compiler is not able to infer the type of the objects inside `NSPointerArray`, since it uses pointers of objects `AnyObject`. For this reason, when you get an object from the array, you must cast it to your object type: | ||
“非类型安全”,在此处的意思是编译器无法隐含 `NSPointerArray` 内部对象的类型,因为它使用的是 `AnyObject` 型对象的指针。因此,当你从数组中获取一个对象时,你需要检查它是否是你所需要的类型: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 无法隐含
NSPointerArray
内部对象的类型 => 无法推断隐含在NSPointerArray
内部的对象的类型 - 获取一个对象时 => 获取对象时
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when you get an object from the array, you must cast it to your object type:
cast 是要把 array 里面的 AnyObject 转成你要的 type 吧
因此,当你从数组中获取一个对象时,你需要把它转成你所需要的类型:
|
||
A possible workaround is creating a new class `WeakRef` with a generic weak property `value`: | ||
一个可行的方案是创建一个新类 `WeakRef` ,它带有一个普通的weak属性 `value`: | ||
|
||
``` | ||
classWeakRef<T>whereT: AnyObject{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
classWeakRef => class WeakRef
|
||
For “not type-safe”, I mean that the compiler is not able to infer the type of the objects inside `NSPointerArray`, since it uses pointers of objects `AnyObject`. For this reason, when you get an object from the array, you must cast it to your object type: | ||
“非类型安全”,在此处的意思是编译器无法隐含 `NSPointerArray` 内部对象的类型,因为它使用的是 `AnyObject` 型对象的指针。因此,当你从数组中获取一个对象时,你需要检查它是否是你所需要的类型: | ||
|
||
``` | ||
ifletfirstObject=array.object(at:0)as?MyClass{// Cast to MyClass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ifletfirstObject => if let firstObject
这一行代码格式不对,同时请检查一下其余代码块的格式
|
||
If you want to clean the array removing the objects with value `nil`, you can write the following method: | ||
如果你想清理数组,去除其中值为 `nil` 的对象,你可以使用下面的方法: | ||
|
||
``` | ||
funccompact(){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
funccompact => func compact
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@zhangqippp @sqrthree 校对完成,zhangqippp翻译的非常棒,校对不足之处,请多多指教~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
校对完毕,翻译的不错,很用心。
|
||
![](https://marcosantadev.com/wp-content/uploads/header-1.jpg) | ||
|
||
In iOS development there are moments where you ask yourself: “To weak, or not to weak, that is the question”. Let’s see how “to weak” with the arrays. | ||
在 iOS 开发中我们经常面临一个问题:“使用弱引用,还是不使用弱引用?”。我们来看一下如何在数组中使用弱引用。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To weak, or not to weak, that is the question 这是在模仿莎士比亚的哈姆雷特的经典台词,建议可以参照台词的翻译效果意译。
在 iOS 开发中我们经常面临一个问题:“用弱引用还是不用,这是一个问题。”。我们来看一下如何在数组中使用弱引用。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
其实我本来想翻译成:“那么问题来了,用弱引用还是不用”😂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be or not to be, that is the question. 原译为"生存还是毁灭,这是一个问题。"
这边其实我也没想出特别好的 😂
|
||
*In this article, I speak about memory management without explaining it since it would be beyond the goal of this article. The [official documentation](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.html) is a good starting point to learn this subject. Then, if you have other doubts, please leave a comment and I’ll reply as soon as possible.* | ||
*在本文中,我会谈到内存管理但是不会解释它,因为这不是本文的主题。 这里的[官方文档](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.html)可以帮助你开始学习内存管理。 如果你有其它疑问,请留言,我会尽快给予回复。* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里的[官方文档]可以帮助你开始学习内存管理。
the 可以不用翻译
[官方文档]是学习内存管理的一个好的起点。
|
||
`Array` is the most popular collection in Swift. By default, it maintains a strong references of its elements. Even if this behaviour is useful most of the time, you might have some scenarios where you would want to use weak references. For this reason, Apple provides an alternative to `Array` which maintains weak references of its elements: **NSPointerArray**. | ||
`Array` 是Swift中使用最多的集合。它会默认的对其元素持有强引用。 这种默认的行为在大多数时候都很有用,但是在某些场景下你可能想要使用弱引用。因此,苹果公司给我们提供了一个 `Array` 的替代品:**NSPointerArray**,这个类对它的元素持有弱引用。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
它会默认的对其元素持有强引用。
默认地
|
||
``` | ||
protocolMyProtocol: class{} | ||
``` | ||
|
||
2. If you want to play with `NSPointerArray`, I suggest you to avoid the Playground since you may have odd behaviours with the retain count. A sample app would be better. | ||
2. 如果你想试用一下 `NSPointerArray`,我建议不要试用 Playground ,因为你可能因为引用计数问题得到一些奇怪的行为。使用一个简单的 app 会更好。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
试用
使用
|
||
For “not type-safe”, I mean that the compiler is not able to infer the type of the objects inside `NSPointerArray`, since it uses pointers of objects `AnyObject`. For this reason, when you get an object from the array, you must cast it to your object type: | ||
“非类型安全”,在此处的意思是编译器无法隐含 `NSPointerArray` 内部对象的类型,因为它使用的是 `AnyObject` 型对象的指针。因此,当你从数组中获取一个对象时,你需要检查它是否是你所需要的类型: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when you get an object from the array, you must cast it to your object type:
cast 是要把 array 里面的 AnyObject 转成你要的 type 吧
因此,当你从数组中获取一个对象时,你需要把它转成你所需要的类型:
|
||
I guess you are not going to use arrays with weak references very often, but it’s not an excuse not to know how to achieve it. In iOS development the memory management is very important to avoid memory leaks, since iOS doesn’t have a garbage collector. ¯\_(ツ)_/¯ | ||
你可能不会经常使用持有弱引用的数组,但是你仍然可以学习如何实现它。在 iOS 开发中,内存管理是非常重要的,我们应该避免内存泄露,因为 iOS 没有垃圾回收器。 ¯\_(ツ)_/¯ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
你可能不会经常使用持有弱引用的数组,但是你仍然可以学习如何实现它。
你可能不会经常使用持有弱引用的数组,但是这不是不去了解其实现原理的理由。
@zhangqippp @sqrthree 校对完成 |
@ZhangRuixiang , @Danny1451 , @sqrthree , 根据校对意见修改完毕 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
很棒,但是有一个小问题还望辛苦修改下哈。文中有多处用斜体了,请参考 wiki 斜体文字使用加粗样式代替
@sqrthree , 斜体修改完毕 |
@zhangqippp 已经 merge 啦~ 快快麻溜发布到掘金专栏然后给我发下链接,方便及时添加积分哟。 |
@sqrthree,翻译完成
issue 1679