# ReactiveCocoa [![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
ReactiveCocoa (RAC) is an Objective-C framework inspired by [Functional Reactive
Programming][]. It provides APIs for **composing and transforming streams of
values**.
If you're already familiar with functional reactive programming or know the basic
premise of ReactiveCocoa, check out the [Documentation][] folder for a framework
overview and more in-depth information about how it all works in practice.
## New to ReactiveCocoa?
ReactiveCocoa is documented like crazy, and there's a wealth of introductory
material available to explain what RAC is and how you can use it.
If you want to learn more, we recommend these resources, roughly in order:
1. [Introduction](#introduction)
1. [When to use ReactiveCocoa](#when-to-use-reactivecocoa)
1. [Framework Overview][]
1. [Basic Operators][]
1. [Header documentation](ReactiveCocoa)
1. Previously answered [Stack Overflow](https://github.com/ReactiveCocoa/ReactiveCocoa/wiki)
questions and [GitHub issues](https://github.com/ReactiveCocoa/ReactiveCocoa/issues?labels=question&state=closed)
1. The rest of the [Documentation][] folder
1. [Functional Reactive Programming on iOS](https://leanpub.com/iosfrp/)
(eBook)
If you have any further questions, please feel free to [file an issue](https://github.com/ReactiveCocoa/ReactiveCocoa/issues/new).
## Introduction
ReactiveCocoa is inspired by [functional reactive
programming](http://blog.maybeapps.com/post/42894317939/input-and-output).
Rather than using mutable variables which are replaced and modified in-place,
RAC provides signals (represented by `RACSignal`) that capture present and
future values.
By chaining, combining, and reacting to signals, software can be written
declaratively, without the need for code that continually observes and updates
values.
For example, a text field can be bound to the latest time, even as it changes,
instead of using additional code that watches the clock and updates the
text field every second. It works much like KVO, but with blocks instead of
overriding `-observeValueForKeyPath:ofObject:change:context:`.
Signals can also represent asynchronous operations, much like [futures and
promises][]. This greatly simplifies asynchronous software, including networking
code.
One of the major advantages of RAC is that it provides a single, unified
approach to dealing with asynchronous behaviors, including delegate methods,
callback blocks, target-action mechanisms, notifications, and KVO.
Here's a simple example:
```objc
// When self.username changes, logs the new name to the console.
//
// RACObserve(self, username) creates a new RACSignal that sends the current
// value of self.username, then the new value whenever it changes.
// -subscribeNext: will execute the block whenever the signal sends a value.
[RACObserve(self, username) subscribeNext:^(NSString *newName) {
NSLog(@"%@", newName);
}];
```
But unlike KVO notifications, signals can be chained together and operated on:
```objc
// Only logs names that starts with "j".
//
// -filter returns a new RACSignal that only sends a new value when its block
// returns YES.
[[RACObserve(self, username)
filter:^(NSString *newName) {
return [newName hasPrefix:@"j"];
}]
subscribeNext:^(NSString *newName) {
NSLog(@"%@", newName);
}];
```
Signals can also be used to derive state. Instead of observing properties and
setting other properties in response to the new values, RAC makes it possible to
express properties in terms of signals and operations:
```objc
// Creates a one-way binding so that self.createEnabled will be
// true whenever self.password and self.passwordConfirmation
// are equal.
//
// RAC() is a macro that makes the binding look nicer.
//
// +combineLatest:reduce: takes an array of signals, executes the block with the
// latest value from each signal whenever any of them changes, and returns a new
// RACSignal that sends the return value of that block as values.
RAC(self, createEnabled) = [RACSignal
combineLatest:@[ RACObserve(self, password), RACObserve(self, passwordConfirmation) ]
reduce:^(NSString *password, NSString *passwordConfirm) {
return @([passwordConfirm isEqualToString:password]);
}];
```
Signals can be built on any stream of values over time, not just KVO. For
example, they can also represent button presses:
```objc
// Logs a message whenever the button is pressed.
//
// RACCommand creates signals to represent UI actions. Each signal can
// represent a button press, for example, and have additional work associated
// with it.
//
// -rac_command is an addition to NSButton. The button will send itself on that
// command whenever it's pressed.
self.button.rac_command = [[RACCommand alloc] initWithSignalBlock:^(id _) {
NSLog(@"button was pressed!");
return [RACSignal empty];
}];
```
Or asynchronous network operations:
```objc
// Hooks up a "Log in" button to log in over the network.
//
// This block will be run whenever the login command is executed, starting
// the login process.
self.loginCommand = [[RACCommand alloc] initWithSignalBlock:^(id sender) {
// The hypothetical -logIn method returns a signal that sends a value when
// the network request finishes.
return [client logIn];
}];
// -executionSignals returns a signal that includes the signals returned from
// the above block, one for each time the command is executed.
[self.loginCommand.executionSignals subscribeNext:^(RACSignal *loginSignal) {
// Log a message whenever we log in successfully.
[loginSignal subscribeCompleted:^{
NSLog(@"Logged in successfully!");
}];
}];
// Executes the login command when the button is pressed.
self.loginButton.rac_command = self.loginCommand;
```
Signals can also represent timers, other UI events, or anything else that
changes over time.
Using signals for asynchronous operations makes it possible to build up more
complex behavior by chaining and transforming those signals. Work can easily be
triggered after a group of operations completes:
```objc
// Performs 2 network operations and logs a message to the console when they are
// both completed.
//
// +merge: takes an array of signals and returns a new RACSignal that passes
// through the values of all of the signals and completes when all of the
// signals complete.
//
// -subscribeCompleted: will execute the block when the signal completes.
[[RACSignal
merge:@[ [client fetchUserRepos], [client fetchOrgRepos] ]]
subscribeCompleted:^{
NSLog(@"They're both done!");
}];
```
Signals can be chained to sequentially execute asynchronous operations, instead
of nesting callbacks with blocks. This is similar to how [futures and promises][]
are usually used:
```objc
// Logs in the user, then loads any cached messages, then fetches the remaining
// messages from the server. After that's all done, logs a message to the
// console.
//
// The hypothetical -logInUser methods returns a signal that completes after
// logging in.
//
// -flattenMap: will execute its block whenever the signal sends a value, and
// returns a new RACSignal that merges all of the signals returned from the block
// into a single signal.
[[[[client
logInUser]
flattenMap:^(User *user) {
// Return a signal that loads cached messages for the user.
return [client loadCachedMessagesForUser:user];
}]
flattenMap:^(NSArray *messages) {
// Return a signal that fetches any remaining messages.
return [client fetchMessagesAfterMessage:messages.lastObject];
}]
subscribeNext:^(NSArray *newMessages) {
NSLog(@"New messages: %@", newMessages);
} completed:^{
NSLog(@"Fetched all messages.");
}];
```
RAC even makes it easy to bind to the result of an asynchronous operation:
```objc
// Creates a one-way binding so that self.imageView.image will be set as the user's
// avatar as soon as it's downloaded.
//
// The hypothetical -fetchUserWithUsername: met
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
iOS-个人开源-app-趣账本,iOS开源项目。已上线appStore。喜欢就给个star吧。Objective-C、AFNetWorking、SDWebImage、MJRefresh、Xib、Masonry.zip 软件开发设计:应用软件开发、系统软件开发、移动应用开发、网站开发C++、Java、python、web、C#等语言的项目开发与学习资料 硬件与设备:单片机、EDA、proteus、RTOS、包括计算机硬件、服务器、网络设备、存储设备、移动设备等 操作系统:LInux、树莓派、安卓开发、微机操作系统、网络操作系统、分布式操作系统等。此外,还有嵌入式操作系统、智能操作系统等。 网络与通信:数据传输、信号处理、网络协议、网络与通信硬件、网络安全网络与通信是一个非常广泛的领域,它涉及到计算机科学、电子工程、数学等多个学科的知识。 云计算与大数据:包括云计算平台、大数据分析、人工智能、机器学习等,云计算是一种基于互联网的计算方式,通过这种方式,共享的软硬件资源和信息可以按需提供给计算机和其他设备
资源推荐
资源详情
资源评论
收起资源包目录
iOS-个人开源-app-趣账本,iOS开源项目.zip (1177个子文件)
RACSignalProvider.d 222B
RACCompoundDisposableProvider.d 190B
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.gitignore 1KB
FMDatabase.h 41KB
RACSignal+Operations.h 32KB
RACmetamacros.h 30KB
metamacros.h 30KB
AFURLSessionManager.h 29KB
AFURLRequestSerialization.h 21KB
AFHTTPRequestOperationManager.h 19KB
AFURLConnectionOperation.h 17KB
AFHTTPSessionManager.h 14KB
RACStream.h 13KB
UIButton+WebCache.h 13KB
FMResultSet.h 12KB
MTLJSONAdapter.h 12KB
AFURLResponseSerialization.h 11KB
SDWebImageManager.h 11KB
UIImageView+WebCache.h 10KB
RACSignal.h 9KB
UIButton+AFNetworking.h 9KB
SDImageCache.h 8KB
AFNetworkReachabilityManager.h 8KB
MTLModel.h 7KB
FMDatabaseAdditions.h 7KB
NSArray+BlocksKit.h 7KB
UIImageView+AFNetworking.h 7KB
RACScheduler.h 7KB
SDWebImageDownloader.h 7KB
MASConstraint.h 6KB
RACSequence.h 6KB
NSURLConnection+BlocksKit.h 6KB
NSOrderedSet+BlocksKit.h 6KB
MASUtilities.h 6KB
UIWebView+AFNetworking.h 6KB
NSObject+BKBlockObservation.h 6KB
FMDatabaseQueue.h 6KB
MTLModel+NSCoding.h 5KB
RACTuple.h 5KB
UIAlertView+BlocksKit.h 5KB
RACCommand.h 5KB
UIAlertView+AFNetworking.h 5KB
NSObject+RACPropertySubscribing.h 5KB
NSObject+BKAssociatedObjects.h 5KB
AFSecurityPolicy.h 5KB
UIImageView+HighlightedWebCache.h 5KB
FMDatabasePool.h 5KB
UIActionSheet+BlocksKit.h 5KB
A2DynamicDelegate.h 5KB
NSObject+A2BlockDelegate.h 5KB
NSObject+BKBlockExecution.h 4KB
NSSet+BlocksKit.h 4KB
AFHTTPRequestOperation.h 4KB
UIGestureRecognizer+BlocksKit.h 4KB
RACKVOChannel.h 4KB
RACEXTScope.h 4KB
NSIndexSet+BlocksKit.h 4KB
NSValueTransformer+MTLPredefinedTransformerAdditions.h 4KB
ReactiveCocoa.h 4KB
NSDictionary+BlocksKit.h 4KB
A2BlockInvocation.h 4KB
MASConstraintMaker.h 4KB
AFNetworkActivityIndicatorManager.h 4KB
SDWebImagePrefetcher.h 4KB
SVProgressHUD.h 4KB
NSObject+RACSelectorSignal.h 4KB
EXTScope.h 3KB
RACEXTRuntimeExtensions.h 3KB
UIProgressView+AFNetworking.h 3KB
MJRefreshComponent.h 3KB
View+MASAdditions.h 3KB
EXTRuntimeExtensions.h 3KB
XYPieChart.h 3KB
BKMacros.h 3KB
SDWebImageDownloaderOperation.h 3KB
UIBarButtonItem+BlocksKit.h 3KB
RACChannel.h 3KB
NSObject+RACLifting.h 3KB
MTLTransformerErrorHandling.h 2KB
MJRefreshConst.h 2KB
UIActivityIndicatorView+AFNetworking.h 2KB
UIRefreshControl+AFNetworking.h 2KB
RACEXTKeyPathCoding.h 2KB
NSObject+A2DynamicDelegate.h 2KB
EXTKeyPathCoding.h 2KB
View+MASShorthandAdditions.h 2KB
UIView+BlocksKit.h 2KB
MTLValueTransformer.h 2KB
RACSubscriptingAssignmentTrampoline.h 2KB
BlocksKit.h 2KB
NSObject+RACKVOWrapper.h 2KB
共 1177 条
- 1
- 2
- 3
- 4
- 5
- 6
- 12
资源评论
妄北y
- 粉丝: 1w+
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功