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

iOS 响应式编程:Swift 中的轻量级状态容器 #2013

Merged
merged 6 commits into from
Aug 17, 2017

Conversation

Abysssea
Copy link
Contributor

@Abysssea Abysssea commented Aug 6, 2017

抱歉,其实周五就弄好了,有点事情,就忘了。。

close #1973

@Abysssea Abysssea changed the title 翻译完成。 iOS 响应式编程:Swift 中的轻量级状态容器 Aug 6, 2017
@linhe0x0 linhe0x0 added the iOS label Aug 7, 2017
@FlyOceanFish
Copy link

@sqrthree 申请校对

@linhe0x0
Copy link
Member

linhe0x0 commented Aug 7, 2017

@FlyOceanFish 好的呢 🍺

Copy link

@FlyOceanFish FlyOceanFish left a comment

Choose a reason for hiding this comment

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

@sqrthree @deepmissea 校对完成


Every iOS & MacOS developer has subtly different opinions on how client architecture should work. From the classic [MVC Pattern](https://developer.apple.com/library/content/documentation/General/Conceptual/DevPedia-CocoaCore/MVC.html) built into Apple's own frameworks (read: Massive View Controllers), to those other MV* patterns (MVP, MVVM), to the scary sounding [Viper](https://www.objc.io/issues/13-architecture/viper/), how do we choose?
在客户端架构如何工作上,每一个 iOS 和 MacOS 开发者都有不同的微妙见解。从最经典的苹果框架所内嵌的

Choose a reason for hiding this comment

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

『每一个 iOS 和 MacOS 开发者都有不同的微妙见解』=>『每一个 iOS 和 MacOS 开发者都有细微的不同见解』


This article won't answer that question for you, because the true answer is that it *depends on context*. What I do want to highlight is a fundamental approach I've been seeing more often that I really like, the **state container**.
这篇文章并不会回答你的问题,因为正确的答案是它是**依据环境而定的**。我想要强调的是一个基本的技巧,我经常看到而且很喜欢,名为**状态容器**。

Choose a reason for hiding this comment

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

『因为正确的答案是它是依据环境而定的』=>『因为正确的答案是依据环境而定的


This article won't answer that question for you, because the true answer is that it *depends on context*. What I do want to highlight is a fundamental approach I've been seeing more often that I really like, the **state container**.
这篇文章并不会回答你的问题,因为正确的答案是它是**依据环境而定的**。我想要强调的是一个基本的技巧,我经常看到而且很喜欢,名为**状态容器**。

Choose a reason for hiding this comment

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

『我想要强调的是一个基本的技巧,我经常看到而且很喜欢』=>『我想要强调的是一个我很喜欢并且经常看到的基本方法』


## What is a State Container?
## 什么是一个状态容器 ?

Choose a reason for hiding this comment

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

『什么是一个状态容器 ?』=>『状态容器是什么?』


Essentially, state containers are just wrappers around pieces of information, gatekeepers for inputs & outputs to protected data. They don't care too much about the type of data or where it originally came from. But they do care, a lot, about when it is **changed**. The central doctrine of a state container is that the effect of any state change should propagate across the application in an **organized** and **predictable** fashion.
实质上,状态容器只是一个围绕信息的封装,是数据安全输入输出的守门人。他们不是特别在意数据的类型和来源。但是他们非常在意的是当数据**改变**的时候。状态容器的中心思想就是,任何状态改变的影响都要以**有组织**并且**可预测**的方式,传播到应用中。

Choose a reason for hiding this comment

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

『守门人』=>『守护者』

1. 我们的初始化方法检查 `UserDefaults.standard` 是否已经包含一个由 `key` 对应的值。
2. 如果我们能加载一个对象,并且它刚好是基本类型,我们可以立即使用它。
3. 如果我们加载的是 `Data`,那么使用 `NSKeyedUnarchiver` 解压,它会被 `NSCoding` 存储,然后我立即使用它。
4. 如果 `UserDefaults.standard` 里没有和 `key` 对应的值,我们就使用已提供的 `defaultValue`

Choose a reason for hiding this comment

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

『和 key对应的值』=>『与key匹配的值』

@@ -51,18 +55,18 @@ Let's start by constructing our fundamental `State` class.
}
}

这个通用类封装了一个任何 `Type` 的 `_value`,通过一个 `key` 关联,并声明了一个提供 `defaultValue` 的初始化器。

Choose a reason for hiding this comment

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

『通用类』=>『基类』


7. If our value conforms to `NSCoding`, we'll convert it to `Data` using `NSKeyedArchiver` and then persist it.

Choose a reason for hiding this comment

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

『吧它』=>『把它』


From API responses to feature toggles to protected keychain items, using a state container pattern is a great way to define structure around critical pieces of information. State containers can easily be used for caching, user preferences, analytics, and anything that needs to stick around between app launches.
在状态容器上管理状态有很多好处。以前放在单例上的数据,或在网络代理中传播的数据,现在已经在高层次上浮现出来。应用程序行为中的所有输入都突然变得清晰可见并且组织严谨。

Choose a reason for hiding this comment

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

『现在已经在高层次上浮现出来』=>『现在已经在高层次上浮现出来并且可见』


## About the Author
状态容器模式让 UI 组件不用担心如何以及何时生成数据,并把焦点转向如何把数据转换成梦幻般的用户体验。

Choose a reason for hiding this comment

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

『并把焦点转向如何把数据转换成梦幻般的用户体验』=>『UI 组件开始把焦点转向如何把数据转换成梦幻般的用户体验』

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这句前面提到了 UI 组件,感觉修改之后有些重复

Choose a reason for hiding this comment

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

嗯 也是,但是老感觉有点不顺畅。『并把焦点』=>『并开始把焦点』 这样感觉能顺畅点吧

@@ -3,36 +3,40 @@
> * 原文作者:[Tyler Tillage](https://www.captechconsulting.com/search#q=Tyler Tillage)
> * 译文出自:[掘金翻译计划](https://github.com/xitu/gold-miner)
> * 本文永久链接:[https://github.com/xitu/gold-miner/blob/master/TODO/state-containers-in-swift.md](https://github.com/xitu/gold-miner/blob/master/TODO/state-containers-in-swift.md)
> * 译者:
> * 译者:[deepmissea](http://deepmissea.blue)
> * 校对者:
Copy link
Member

Choose a reason for hiding this comment

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

校对者信息需要加一下。


This isn't a new concept, and it's not a toolkit you can build your entire app with. The state container idea is versatile enough to fit into any app architecture without imposing too many rules. But it's a powerful approach that is at the crux of popular libraries like [ReactiveReSwift](https://github.com/ReSwift/ReactiveReSwift), which is itself based on [ReSwift](https://github.com/ReSwift/ReSwift), which is itself based on [Redux](https://github.com/reactjs/redux), which is itself based on [Flux](https://github.com/facebook/flux)...and the list goes on. The success and sheer number of these frameworks speaks to the efficacy of the state container pattern in modern mobile applications.
这并不是一个新的概念,而且它也不是一个你可以集成到整个应用的工具包。状态容器的理念是非常通用的,它可以融入进任何应用程序架构,而无需太多的附加规则。但是它是一个强大的方法,是很多流行库(比如[ReactiveReSwift](https://github.com/ReSwift/ReactiveReSwift))的核心,比如 [ReSwift](https://github.com/ReSwift/ReSwift)[Redux](https://github.com/reactjs/redux)[Flux](https://github.com/facebook/flux) 等等,这些框架的成功和绝对数量说明了状态容器模式在现代移动应用中的有效性。
Copy link
Member

Choose a reason for hiding this comment

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

ReSwiftReduxFlux 』=>『ReSwiftReduxFlux

@@ -3,36 +3,40 @@
> * 原文作者:[Tyler Tillage](https://www.captechconsulting.com/search#q=Tyler Tillage)
> * 译文出自:[掘金翻译计划](https://github.com/xitu/gold-miner)
> * 本文永久链接:[https://github.com/xitu/gold-miner/blob/master/TODO/state-containers-in-swift.md](https://github.com/xitu/gold-miner/blob/master/TODO/state-containers-in-swift.md)
> * 译者:
> * 译者:[deepmissea](http://deepmissea.blue)
> * 校对者:

Choose a reason for hiding this comment

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

@linhe0x0 linhe0x0 merged commit 66ee2e9 into xitu:master Aug 17, 2017
@linhe0x0
Copy link
Member

@DeepMissea 已经 merge 啦~ 快快麻溜发布到掘金专栏然后给我发下链接,方便及时添加积分哟。

RichardLeeH pushed a commit to RichardLeeH/gold-miner that referenced this pull request Aug 18, 2017
…slate

# By sqrtthree (47) and others
# Via GitHub (4) and others
* 'master' of https://github.com/xitu/gold-miner: (164 commits)
  update advices from @sqrthree
  ✨ Create how-to-do-proper-tree-shaking-in-webpack-2.md
  update for @LouisaNikita's proofreading
  Update why-i-havent-fixed-your-issue-yet.md
  Update why-i-havent-fixed-your-issue-yet.md
  iOS 响应式编程:Swift 中的轻量级状态容器 (xitu#2013)
  自定义 Babel 和 ESLint 插件是如何提高生产率与用户体验的 (xitu#2025)
  ✨ Create web-fonts-when-you-need-them-when-you-dont.md
  ✨ Create under-the-hood-of-futures-and-promises-in-swift.md
  update proofreader @sunui
  update for @sunui's proofreading
  ✨ Create how-to-write-a-perfect-error-message.md
  ✨ Create how-i-do-developer-ux-at-google.md
  ✨ Create v8-behind-the-scenes-november-edition.md
  Other mentions
  fix typos (xitu#2058)
  🚀 添加文章『深度学习系列4: 为什么你需要使用嵌入层』到文章列表
  🚀 添加文章『使用 Node.js 搭建一个 API 网关』到文章列表
  🚀 添加文章『AI 能解决你的 UX 设计问题吗?』到文章列表
  🚀 添加文章『理解 Service Worker』到文章列表
  ...
@Abysssea
Copy link
Contributor Author

cdadar pushed a commit to cdadar/gold-miner that referenced this pull request Dec 8, 2017
@Abysssea Abysssea deleted the translate branch January 1, 2018 18:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

iOS 响应式编程:Swift 中的轻量级状态容器
3 participants