Skip to content

1329: Translation for LanguageGuide / optional-chaining.md ⭐️⭐️⭐️⭐️⭐️ #1387

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

Conversation

MOXCOOT
Copy link
Collaborator

@MOXCOOT MOXCOOT commented Sep 23, 2024

Translation for LanguageGuide / optional-chaining

@MOXCOOT MOXCOOT requested a review from zhangqifan September 23, 2024 08:14
@yongfrank yongfrank linked an issue Sep 24, 2024 that may be closed by this pull request
@yongfrank yongfrank changed the title 1329:Translation for LanguageGuide / optional-chaining.md ⭐️⭐️⭐️⭐️⭐️ 1329: Translation for LanguageGuide / optional-chaining.md ⭐️⭐️⭐️⭐️⭐️ Sep 24, 2024
@yongfrank yongfrank requested review from saitjr and removed request for zhangqifan October 4, 2024 13:07
Copy link
Collaborator

@Channe Channe left a comment

Choose a reason for hiding this comment

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

我换一篇没有认领的来 Review


为了反映可选链式调用可以在空值(`nil`)上调用的事实,不论这个调用的属性、方法及下标返回的值是不是可选值,它的返回结果都是一个可选值。你可以利用这个返回值来判断你的可选链式调用是否调用成功,如果调用有返回值则说明调用成功,返回 `nil` 则说明调用失败。

特别地,可选链式调用的返回结果与原本的返回结果具有相同的类型,但是被包装成了一个可选值。例如,使用可选链式调用访问属性,当可选链式调用成功时,如果属性原本的返回结果是 `Int` 类型,则会变为 `Int?` 类型。
Copy link
Member

Choose a reason for hiding this comment

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

具体来说,可选链调用的结果,除了被包装成了一个可选值外,类型与预期的返回值类型是相同的。例如,正常情况下返回 Int 的属性,在通过可选链访问时,返回的是 Int?


特别地,可选链式调用的返回结果与原本的返回结果具有相同的类型,但是被包装成了一个可选值。例如,使用可选链式调用访问属性,当可选链式调用成功时,如果属性原本的返回结果是 `Int` 类型,则会变为 `Int?` 类型。

下面几段代码将解释可选链式调用和强制展开的不同。
Copy link
Member

Choose a reason for hiding this comment

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

下面几段代码将演示可选链式调用和强制展开的不同,以及如何判断可选链是否调用成功。


```swift
class Person {
var residence: Residence?
var residence: Residence?
Copy link
Member

Choose a reason for hiding this comment

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

这里为什么把四个空格,换成了 Tab?

In the code below, `john` has a `residence` property value of `nil`:
`Residence` 有一个 `Int` 类型的属性 `numberOfRooms`,其默认值为 `1`。`Person` 具有一个可选的 `residence` 属性,其类型为 `Residence?`。

假如你创建了一个新的 `Person` 实例,它的 `residence` 属性由于是是可选型而将初始化为 `nil`,在下面的代码中,`john` 有一个值为 `nil` 的 `residence` 属性:
Copy link
Member

Choose a reason for hiding this comment

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

这一句的逗号有问题,注意用中文逗号

Copy link
Member

Choose a reason for hiding this comment

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

假如你创建了一个新的 Person 实例,由于 residence 属性是可选型,所以初始值是 nil。就像下面的代码中,john 有一个值为 nilresidence 属性:

by placing an exclamation point after `residence` to force the unwrapping of its value,
you trigger a runtime error,
because there's no `residence` value to unwrap:
如果使用叹号(`!`)强制展开获得这个 `john` 的 `residence` 属性中的 `numberOfRooms` 值,会触发运行时错误,因为这时 `residence` 没有可以展开的值:
Copy link
Member

Choose a reason for hiding this comment

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

如果使用叹号(!)对 johnresidence 属性中的 numberOfRooms 值强制解包,会触发运行时错误,因为这时 residence 没有可以解包的值:

@MOXCOOT MOXCOOT requested a review from saitjr October 19, 2024 13:56

```swift
if let roomCount = john.residence?.numberOfRooms {
print("John's residence has \(roomCount) room(s).")
print("John's residence has \(roomCount) room(s).")
print("John's residence has \(roomCount) room(s).")
Copy link
Member

Choose a reason for hiding this comment

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

这个地方,原文不是只打印了一次吗?怎么有两个 print

```

`john.residence` 现在包含一个实际的 `Residence` 实例,而不再是 `nil`。如果你试图使用先前的可选链式调用访问 `numberOfRooms`,它现在将返回值为 `1` 的 `Int?` 类型的值:
`john.residence` 现在包含一个实际的 `Residence` 实例,而不再是 `nil`。如果你试图使用先前的可选链式调用访问 `numberOfRooms`,它现在将返回值为 `1` 的 `Int?` 类型的值:
Copy link
Member

Choose a reason for hiding this comment

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

这里也是,为啥有两句

要注意的是,即使 `numberOfRooms` 是非可选的 `Int` 时,这一点也成立。只要使用可选链式调用就意味着 `numberOfRooms` 会返回一个 `Int?` 而不是 `Int`。

可以将一个 `Residence` 的实例赋给 `john.residence`,这样它就不再是 `nil` 了:
// 打印 “Unable to retrieve the number of rooms.”
```
Copy link
Member

Choose a reason for hiding this comment

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

这三个 ```,搞错地方了吧。是不是这个文件解冲突解出问题了

@MOXCOOT MOXCOOT force-pushed the 1329-languageguide--optional-chainingmd branch 3 times, most recently from 8b751d1 to 8017283 Compare October 20, 2024 14:56
…MOXCOOT/the-swift-programming-language-in-chinese into 1329-languageguide--optional-chainingmd
@MOXCOOT MOXCOOT force-pushed the 1329-languageguide--optional-chainingmd branch from 8017283 to 306960c Compare October 20, 2024 14:57
@yongfrank yongfrank merged commit c194609 into SwiftGGTeam:swift-6-beta-translation Oct 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Completed
Development

Successfully merging this pull request may close these issues.

LanguageGuide / optional-chaining.md ⭐️⭐️⭐️⭐️⭐️
4 participants