-
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
SwiftUI 2 应用生命周期的终极指导 #7939
SwiftUI 2 应用生命周期的终极指导 #7939
Conversation
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.
Excellent Translation 不过有部分地方如 activity 的翻译就过头了 @lsvih @zhuzilin
- by 苏苏的 霜羽 Hoarfroster
|
||
For the longest time, iOS developers have used `AppDelegate`s as the main entry point for their applications. With the launch of SwiftUI2 at WWDC 2020, Apple has introduced a new application life cycle that (almost) completely does away with `AppDelegate`, making way for a DSL-like approach. | ||
在很长一段时间里,`AppDelegate` 都是 iOS 开发者们的应用的入口。随着 SwiftUI2 在 WWDC 2020 上发布,苹果公司引入了一个新的应用生命周期。新的生命周期几乎(几乎)完全与 `AppDelegate` 无关,为类 DSL 的方法铺平了道路。 |
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.
SwiftUI 2 + 类..名词不必要加助词吧:)
在很长一段时间里,`AppDelegate` 都是 iOS 开发者们的应用的入口。随着 SwiftUI2 在 WWDC 2020 上发布,苹果公司引入了一个新的应用生命周期。新的生命周期几乎(几乎)完全与 `AppDelegate` 无关,为类 DSL 的方法铺平了道路。 | |
在很长一段时间里,`AppDelegate` 都是 iOS 开发者们的应用的入口。随着 SwiftUI 2 在 WWDC 2020 上发布,苹果公司引入了一个新的应用生命周期。新的生命周期几乎(几乎)完全与 `AppDelegate` 无关,为类 DSL 方法铺平了道路。 |
|
||
In this article, I will discuss why this change was introduced, and how you can make use of the new life cycle in new or existing apps. | ||
在本文中,我会讨论引入新的生命周期的原因,以及你该如何在新旧应用中使用它。 |
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 Swift compiler will recognize a type annotated with the @main attribute as providing the entry point for a program. Types marked with @main have a single implicit requirement: declaring a static main() method. | ||
> Swift编译器将识别标注了 @main 属性的类型为程序的入口。标有 @main 的类型有一个隐式要求:类型内部需要声明一个静态 main() 方法。 |
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.
> Swift编译器将识别标注了 @main 属性的类型为程序的入口。标有 @main 的类型有一个隐式要求:类型内部需要声明一个静态 main() 方法。 | |
> Swift编译器将识别标注了 `@main` 属性的类型为程序的入口。标有 `@main` 的类型有一个隐式要求:类型内部需要声明一个静态 `main()` 方法。 |
|
||
When creating a new SwiftUI app, the app’s main class looks like this: | ||
创建新的 SwiftUI 应用时,应用的 main class 如下所示: |
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.
main class 可以翻译啊,或者至少翻译 class 或者至少加代码标记嗷!
创建新的 SwiftUI 应用时,应用的 main class 如下所示: | |
创建新的 SwiftUI 应用时,应用的 `main` class 如下所示: |
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.
我还是翻译成主类好了~
@@ -76,21 +74,20 @@ extension ParsableCommand { | |||
} | |||
``` | |||
|
|||
If all this sounds a bit complicated, the good news is you don’t actually have to worry about it when creating a new SwiftUI application: just make sure to select **SwiftUI App** in the **Life Cycle** dropdown when creating your app, and you’re done: | |||
如果上述这些听起来有点复杂,好消息是实际上在创建新的SwiftUI应用程序时你不必关心它:只需确保在 **Life Cycle** 下拉菜单中选择 **SwiftUI 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.
如果上述这些听起来有点复杂,好消息是实际上在创建新的SwiftUI应用程序时你不必关心它:只需确保在 **Life Cycle** 下拉菜单中选择 **SwiftUI App** 来创建你的应用程序就行了: | |
如果上述这些听起来有点复杂,好消息是实际上在创建新的 SwiftUI 应用程序时你不必关心它:只需确保在 **Life Cycle** 下拉菜单中选择 **SwiftUI App** 来创建你的应用程序就行了: |
@@ -204,7 +201,7 @@ struct ColorDetailsView: View { | |||
} | |||
``` | |||
|
|||
To allow continuation of this activity, you can register a `onContinueUserActivity` closure in your top-level navigation view, like this: | |||
为了允许继续进行此活动,你可以在最顶层的导航视图中注册 `onContinueUserActivity` 闭包,如下所示: |
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.
@PassionPenguin 嗯,的确是。我翻译的时候也觉得这个活动很怪...但是又觉得写成“用户 activity” 就太中英混杂了...我还是保留 activity 吧~
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.
话说,我感觉这个很类似于 Android 的 Activity,也是一个生命周期,例如
package io.hoarfroster.android
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
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.
感觉直接翻译
为了允许继续进行此活动,你可以在最顶层的导航视图中注册 `onContinueUserActivity` 闭包,如下所示: | |
为了允许继续进行此 activity,你可以在最顶层的导航视图中注册 `onContinueUserActivity` 闭包,如下所示: |
|
||
Again, the new application life cycle model makes this easier by providing two modifiers that allow you to advertise an activity and later continue it. | ||
同样,新的应用生命周期模型通过提供两个 modifier 使你更容易实现这一点。这些 modifier 使你可以公示活动并随后继续进行。 |
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.
?
|
||
If your app uses `NSUserActivity` to [integrate with](https://developer.apple.com/documentation/foundation/nsuseractivity) Siri, Handoff, or Spotlight, you need to handle user activity continuation. | ||
如果你的应用使用 `NSUserActivity` 来[集成](https://developer.apple.com/documentation/foundation/nsuseractivity) Siri、Handoff 或 Spotlight,你需要处理用户继续进行的活动。 |
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.
?
|
||
Not all of `AppDelegate`’s callbacks are supported by the new application life cycle (yet). If none of the above meets your needs, you might require an `AppDelegate` after all. | ||
新的应用声明周期(截止当前)并非支持 `AppDelegate`的所有回调函数。如果上述这些都不满足你的需求,你可能还是需要一个 `AppDelegate`。 |
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.
新的应用声明周期(截止当前)并非支持 `AppDelegate`的所有回调函数。如果上述这些都不满足你的需求,你可能还是需要一个 `AppDelegate`。 | |
新的应用声明周期(截止当前)并非支持 `AppDelegate` 的所有回调函数。如果上述这些都不满足你的需求,你可能还是需要一个 `AppDelegate`。 |
|
||
Another reason you might require an AppDelegate is if you use any third-party SDKs that make use of [method swizzling](https://pspdfkit.com/blog/2019/swizzling-in-swift/) to inject themselves into the application life cycle. [Firebase](https://firebase.google.com/) is a [well-known case](https://stackoverflow.com/a/62633158/281221). | ||
另一个需要 AppDelegate 的原因是你使用的第三方 SDK 会使用 [method swizzling](https://pspdfkit.com/blog/2019/swizzling-in-swift/) 来把它们注入应用生命周期。[Firebase](https://firebase.google.com/) 就是一个[著名的例子](https://stackoverflow.com/a/62633158/281221)。 |
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.
另一个需要 AppDelegate 的原因是你使用的第三方 SDK 会使用 [method swizzling](https://pspdfkit.com/blog/2019/swizzling-in-swift/) 来把它们注入应用生命周期。[Firebase](https://firebase.google.com/) 就是一个[著名的例子](https://stackoverflow.com/a/62633158/281221)。 | |
另一个需要 `AppDelegate` 的原因是你使用的第三方 SDK 会使用 [method swizzling](https://pspdfkit.com/blog/2019/swizzling-in-swift/) 来把它们注入应用生命周期。[Firebase](https://firebase.google.com/) 就是一个[典型的例子](https://stackoverflow.com/a/62633158/281221)。 |
@PassionPenguin 感谢校对~我会尽快改正过来的。 |
@lsvih 校对认领 |
@zenblo 好的 |
@@ -2,24 +2,24 @@ | |||
> * 原文作者:[Peter Friese](https://peterfriese.dev/) | |||
> * 译文出自:[掘金翻译计划](https://github.com/xitu/gold-miner) | |||
> * 本文永久链接:[https://github.com/xitu/gold-miner/blob/master/article/2021/ultimate-guide-to-swiftui2-application-lifecycle.md](https://github.com/xitu/gold-miner/blob/master/article/2021/ultimate-guide-to-swiftui2-application-lifecycle.md) | |||
> * 译者: | |||
> * 译者:[zhuzilin](https://github.com/zhuzilin) | |||
> * 校对者: |
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 the longest time, iOS developers have used `AppDelegate`s as the main entry point for their applications. With the launch of SwiftUI2 at WWDC 2020, Apple has introduced a new application life cycle that (almost) completely does away with `AppDelegate`, making way for a DSL-like approach. | ||
在很长一段时间里,`AppDelegate` 都是 iOS 开发者们的应用的入口。随着 SwiftUI2 在 WWDC 2020 上发布,苹果公司引入了一个新的应用生命周期。新的生命周期几乎(几乎)完全与 `AppDelegate` 无关,为类 DSL 的方法铺平了道路。 |
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.
AppDelegate
都是 iOS 开发者们的应用的入口
=>iOS 开发者们都是使用 AppDelegate
作为应用的主要入口
|
||
One of the first questions that we need to answer is, how can we tell the compiler about the entry point to our application? [SE-0281](https://github.com/apple/swift-evolution/blob/master/proposals/0281-main-attribute.md) specifies how **Type-Based Program Entry Points** work: | ||
我们的第一个问题是,该如何告诉编译器哪里是应用的入口呢?[SE-0281](https://github.com/apple/swift-evolution/blob/master/proposals/0281-main-attribute.md)详述了**基于类型的程序入口(Type-Based Program Entry Points)**的工作方式: |
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 Swift compiler will recognize a type annotated with the @main attribute as providing the entry point for a program. Types marked with @main have a single implicit requirement: declaring a static main() method. | ||
> Swift编译器将识别标注了 @main 属性的类型为程序的入口。标有 @main 的类型有一个隐式要求:类型内部需要声明一个静态 main() 方法。 |
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.
Swift编译器
=>Swift 编译器
添加空格
@@ -76,21 +74,20 @@ extension ParsableCommand { | |||
} | |||
``` | |||
|
|||
If all this sounds a bit complicated, the good news is you don’t actually have to worry about it when creating a new SwiftUI application: just make sure to select **SwiftUI App** in the **Life Cycle** dropdown when creating your app, and you’re done: | |||
如果上述这些听起来有点复杂,好消息是实际上在创建新的SwiftUI应用程序时你不必关心它:只需确保在 **Life Cycle** 下拉菜单中选择 **SwiftUI 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.
好消息是实际上在创建新的SwiftUI应用程序时你不必关心它
=>好消息是实际上在创建新的 SwiftUI 应用程序时你不必关心它
@PassionPenguin @zenblo 感谢两位的校对,已更新。 |
@zhuzilin 已经 merge 啦~ 快快麻溜发布到掘金然后给我发下链接,方便及时添加积分哟。 掘金翻译计划有自己的知乎专栏,你也可以投稿哈,推荐使用一个好用的插件。 |
译文翻译完成,resolve #7934