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!

ReactiveCocoa Tutorial的更多相关文章

  1. ReactiveCocoa入门教程——第一部分

      ReactiveCocoa iOS 翻译    2015-01-22 02:33:37    11471    6    15 本文翻译自RayWenderlich  ReactiveCocoa ...

  2. ReactiveCocoa入门-part2

    ReactiveCocoa是一个框架,它能让你在iOS应用中使用函数响应式编程(FRP)技术.在本系列教程的第一部分中,你学到了如何将标准的动作与事件处理逻辑替换为发送事件流的信号.你还学到了如何转换 ...

  3. ReactiveCocoa入门-part1

    作为一个iOS开发者,你写的每一行代码几乎都是在响应某个事件,例如按钮的点击,收到网络消息,属性的变化(通过KVO)或者用户位置的变化(通过CoreLocation).但是这些事件都用不同的方式来处理 ...

  4. 快快快!27个提升效率的iOS开源库推荐

    文章来源:http://www.csdn.net/article/2015-07-21/2825264-27-ios-open-source-libraries/1 我热爱开源,更喜爱那些花费宝贵的业 ...

  5. 快快快!27个提升效率的iOS开源库推荐(转)

    CSDN移动将持续为您优选移动开发的精华内容,共同探讨移动开发的技术热点话题,涵盖移动应用.开发工具.移动游戏及引擎.智能硬件.物联网等方方面面.如果您想投稿.参与内容翻译工作,或寻求近匠报道,请发送 ...

  6. iOS----精品开源库-开发强力助攻

    30个精品iOS开源库,超强助攻 你不会想错过他们,真的. 我爱开源.  文章的尾部你会看到一个太长不看的版本——一个简单的列表,只有标题和到项目的链接.如果你发现这篇文章是有用的,把它和你的iOS开 ...

  7. iPhone Tutorials

    http://www.raywenderlich.com/tutorials This site contains a ton of fun written tutorials – so many t ...

  8. ReactiveCocoa基础知识内容

    本文记录一些关于学习ReactiveCocoa基础知识内容,对于ReactiveCocoa相关的概念如果不了解可以网上搜索:RACSignal有很多方法可以来订阅不同的事件类型,ReactiveCoc ...

  9. ReactiveCocoa 和 MVVM 入门 (转)

    翻译自ReactiveCocoa and MVVM, an Introduction. 文中引用的 Gist 可能无法显示.为了和谐社会, 请科学上网. MVC 任何一个正经开发过一阵子软件的人都熟悉 ...

随机推荐

  1. QueryString传值的加密与解密方法 .

    //加密 Response.Redirect("DetailInfo.aspx?id=" + Convert.ToBase64String(System.Text.Encoding ...

  2. CI 笔记,使用 json的参考文档(废弃)

    Json的处理转换, Json转换步骤, a)         先设置json为空字符串, b)         While循环,遍历, While(!!$row = mysql_fetch_arra ...

  3. "Unable to get image data from canvas because the canvas has been tainted by cross-origin data"问题产生原因及解决办法

    一.问题描述: 在支持html5的浏览器中运行javascript脚本,脚本主要是操作网页上的标签canvas,出错的操作为, getImageData(img,……), chrome 下出错信息为: ...

  4. sass学习--什么是sass

    1.预备知识--什么是 CSS 预处理器 CSS 预处理器定义了一种新的语言,其基本思想是,用一种专门的编程语言,为 CSS 增加了一些编程的特性,将 CSS 作为目标生成文件,然后开发者就只要使用这 ...

  5. centos7 更改语言

    vim /etc/locale.conf 修改 LANG="en_US.UTF-8"

  6. python路径函操作

    #判断是否为文件 os.path.isfile   #判断是否为目录 os.path.isdir   #返回文件名 os.path.basename(path)   #返回文件目录 os.path.d ...

  7. Swift(一,创建对象,类型推导,基本运算,逻辑,字符串,数组,字典)

    swift用起来刚开始感觉有点怪怪的,但用了一段时间觉得还是挺好用的,哈哈.毕竟都是要有一个过程的嘛. 我就写一些自己在使用swift的时候的注意点吧,如有不正之处,还请指正! 一.在开发中优先使用常 ...

  8. typedef与define

    一.typedef用法 typedef常用来定义一个标识符及关键字的别名,它生效是在语言编译过程,但它并不实际分配内存空间.typedef可以增强程序的可读性,以及标识符的灵活性,但它也有“非直观性” ...

  9. linq 多个left join 和 sql union all -> linq union 方法

     (   from s in Base_SysMenus   join r in Base_RoleRights on s.Menu_Id equals r.Menu_Id into temp   f ...

  10. 金山网络2014春季Android实习生招聘-成都站-笔试第一题

    实现单例模式,并实现方法int getResult(float a),将a*8后返回. package jinshanwangluo.exam; /** * @author guoxm * @date ...