ReactiveCocoa Tutorial

ReactiveCocoa Tutorial – The Definitive Introduction: Part 1/2

ReactiveCocoa教程——明确的介绍:第一部分(共两部分)

As an iOS developer, nearly every line of code you write is in reaction to some event; a button tap, a received network message, a property change (via Key Value Observing) or a change in user’s location via CoreLocation are all good examples. However, these events are all encoded in different ways; as actions, delegates, KVO, callbacks and others.

作为一个 iOS 开发者,几乎你写的每一个行代码都是在针对某些事件的响应。点击一个按钮、接受一个网络消息、改变一个属性(通过键值观察)或者通过 CoreLoation 改变用户位置都是很好的例子。然而这些事件都是以不同的形式编码,例如动作、代表、键值观察、回调等等。

ReactiveCocoa defines a standard interface for events, so they can be more easily chained, filtered and composed using a basic set of tools.

ReactiveCocoa 给事件定义了一个标准的接口,所以它们可以更容易地连接,过滤并使用一系列基本工具组成。

Sound confusing? Intriguing? … Mind blowing? Then read on :]

听起来很疑惑?有趣?兴奋?还是继续读下去吧☺

ReactiveCocoa combines a couple of programming styles: Functional Programming which makes use of higher order functions, i.e. functions which take other functions as their arguments Reactive Programming which focuses of data-flows and change propagation

ReacticeCocoa 结合了好几种编码风格: ·函数式编程——利用高阶函数,即函数将其它函数作为其参数 ·反应性编程——注重于数据流和改变传播

For this reason, you might hear ReactiveCocoa described as a Functional Reactive Programming (or FRP) framework.

由于这个原因,你可能会听说过ReactiveCocoa被描述成功能反应性编程(FRC)框架。

Rest assured, that is as academic as this tutorial is going to get! Programming paradigms are a fascinating subject, but the rest of this ReactiveCocoa tutorials focuses solely on the practical value, with work-through examples instead of academic theories.

毋庸置疑,这就是本教程预期想要描述的学术知识。编程范式是一个吸引人的话题,但本 ReactiveCocoa 教程的其余部分完全集中于实用价值,用的是实际工作的例子而不是学术理论。

The Reactive Playground

Reactive Playground 应用程序

Throughout this ReactiveCocoa tutorial, you’ll be introducing reactive programming to a very simple example application, the ReactivePlayground. Download the starter project, then build and run to verify you have everything set up correctly.

通过此 ReactiveCocoa 教程,你将会在一个非常简单的示例应用程序即 Reactive Playground 下被引入反应式编程的概念。下载启动项目,然后构建运行来验证你之前一切正确的设置。

ReactivePlayground is a very simple app that presents a sign-in screen to the user. Supply the correct credentials, which are, somewhat imaginatively, user for the username, and password for the password, and you’ll be greeted by a picture of a lovely little kitten.

ReactivePlayground 是一个非常简单的应用程序,给用户提供了一个登录屏幕,提交正确的凭证,多少想象一下,(下面照片中)user输入用户名 password输入密码,接下来你就会被一个可爱的小猫咪照片问候。

 

Awww! How cute!

哇噢噢噢!多么可爱啊!

Right now it’s a good point to spend a little time looking through the code of this starter project. It is quite simple, so it shouldn’t take long.

好吧现在是一个很好的时机去花点时间去浏览这个启动项目的代码。它是如此简单所以不会占用你多少时间。

Open RWViewController.m and take a look around. How quickly can you identify the condition that results in the enabling of the Sign In button? What are the rules for showing / hiding the signInFailure label? In this relatively simple example, it might take only a minute or two to answer these questions. For a more complex example, you should be able to see how this same type of analysis might take quite a bit longer.

打开 RWViewController.m 文件然后把文件到处浏览一下,你能多快就确定登录按钮所授权允许的登录条件?显示或者隐藏登录失败标签的规则是什么?在这个相对简单的例子中,这个问题只需要一两分钟的时间来回答。相对于更复杂的示例,你可能会需要花费相当长的时间去理解同类型的分析。

With the use of ReactiveCocoa, the underlying intent of the application will become a lot clearer. It’s time to get started!

使用 ReactiveCocoa,底层应用程序将会变得更加清晰可见,是时候开始了!

Adding the ReactiveCocoa Framework

添加ReactiveCocoa框架

The easiest way to add the ReactiveCocoa framework to your project is via CocoaPods. If you’ve never used CocoaPods before it might make sense to follow the Introduction To CocoaPods tutorial on this site, or at the very least run through the initial steps of that tutorial so you can install the prerequisites.

通过 CocoaPods 是给你的项目添加 ReactiveCocoa 框架最简单的方法。如果您以前从未使用过 CocoaPods,那么去学习这个网站介绍 CocoaPods 教程是很有意义的,或者至少浏览该教程教的初始步骤,这样你就可以具备先决条件。

Note: If for some reason you don’t want to use CocoaPods you can still use ReactiveCocoa, just follow the Importing ReactiveCocoa steps in the documentation on GitHub.

注意:如果出于某些原因你不想使用 CocoaPods 你仍然可以使用 ReactiveCocoa,只要按照GitHub文档中导入 ReactiveCocoa 的步骤就行了。

If you still have the ReactivePlayground project open in Xcode, then close it now. CocoaPods will create an Xcode workspace, which you’ll want to use instead of the original project file. Open Terminal. Navigate to the folder where your project is located and type the following:

如果你还有 ReactivePlayground 项目在打开的Xcode中,接着马上关闭它。CocoaPods 将会创建一个你想要用来代替原始项目文件的新的 Xcode 工作区,打开终端。前往你的项目所在的文件夹然后敲入如下命令:

touch Podfile

open -e Podfile

This creates an empty file called Podfile and opens it with TextEdit. Copy and paste the following lines into the TextEdit window:

这将创建一个名为 Podfile 的空文件,然后用 TextEdit 打开它。复制粘贴下面几行到 TextEdit 窗口:

platform :ios, '7.0'

pod 'ReactiveCocoa', '2.1.8'

This sets the platform to iOS, the minimum SDK version to 7.0, and adds the ReactiveCocoa framework as a dependency.

这将设置平台为IOS,要求最低的版本是7.0,并添加 ReactiveCocoa 框架作为一个依赖项。

Once you’ve saved this file, go back to the Terminal window and issue the following command:

一旦你保存这个文件,返回到终端窗口并发出以下命令:

pod install

You should see an output similar to the following:

您应该看到一个类似于下面的输出结果:

Analyzing dependencies

Downloading dependencies

Installing ReactiveCocoa (2.1.8)

Generating Pods project

Integrating client project

 

[!] From now on use `RWReactivePlayground.xcworkspace`.

This indicates that the ReactiveCocoa framework has been downloaded, and CocoaPods has created an Xcode workspace to integrate the framework into your existing application.

这表明 ReactiveCocoa 框架已经被下载,和 CocoaPods 已经创建了一个集成这个框架到你现有应用程序的 Xcode 工作区。

Open up the newly generated workspace, RWReactivePlayground.xcworkspace, and look at the structure CocoaPods created inside the Project Navigator:

打开新生成的工作区,RWReactivePlayground.xcworkspace 看看 CocoaPods 在项目导航内部创建的结构。

 

You should see that CocoaPods created a new workspace and added the original project, RWReactivePlayground, together with a Pods project that includes ReactiveCocoa. CocoaPods really does make managing dependencies a breeze!

转载于:https://www.cnblogs.com/XCoderLiu/p/3602414.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值