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. cas系列(一)--cas单点登录基本原理

    (这段时间打算做单点登录,因此研究了一些cas资料并作为一个系列记录下来,一来可能会帮助一些人,二来对我自己所学知识也是一个巩固.) 一.为什么要实现单点登录 随着信息化不断发展,企业的信息化过程是一 ...

  2. jQuery HTML CSS 方法

    jQuery HTML / CSS 方法 下面的表格列出了所有用于处理 HTML 和 CSS 的 jQuery 方法. 下面的方法适用于 HTML 和 XML 文档.除了:html() 方法. 方法 ...

  3. 类库探源——System.Environment

    Environment 类: 提供有关当前环境和平台的信息以及操作它们的方法.此类不能被继承. 命名空间: System 程序集:   mscorlib.dll 继承关系: 常用属性(字段)和方法: ...

  4. POJ 1631 Bridging signals(LIS O(nlogn)算法)

    Bridging signals Description 'Oh no, they've done it again', cries the chief designer at the Waferla ...

  5. Cocos2dx开发(4)——Windows环境创建Cocod2dx 3.2第一个项目HelloWorld

    本文内容:cocos2dx+VS2013环境下创建项目,部分代码简析.会的朋友可以略过. 前面简单安装了几个环境,程序完全可以顺利跑起来(其他的cocos-stadio这些需要用到再装) 1.命令行形 ...

  6. LVS单机测试不负载

    LVS单机测试不负载 1.困惑 当我们在个人PC上搭建虚拟机(Vmware)做LVS负载实验的时候,我们不论是在个人浏览器或者其他虚拟机上访问LVS的VIP都会出现上时间刷新都出现同一个页面的情况. ...

  7. centos下配置多个tomcat同时运行

    首先安装好jdk,下载好tomcat,我的是apache-tomcat-7.0.50,不用专门配置CATALINA_2_BASE,CATALINA_2_HOME等环境变量. 把tomcat解压到lin ...

  8. js 组件的写法

    var Test1 = function(){ var name = ""; this.setName = function(username){ name = username; ...

  9. JQ插件ajaxFileUpload、php实现图片,数据同时上传

    代码结构如下: 1.HTML代码,没必要解释了. <!DOCTYPE html> <html> <head> <meta charset="UTF- ...

  10. JS+CSS实现选项卡功能

    [小小一记] 首先我们写一个选项卡的结构出来,包括tab和content: 首先是tab: <ul class="ttitle-box-tabs" id="tabs ...