# ReactiveObjC
_NOTE: This is legacy introduction to the Objective-C ReactiveCocoa, which is
now known as ReactiveObjC. For the updated version that uses Swift, please see
[ReactiveCocoa][] or [ReactiveSwift][]_
ReactiveObjC (formally ReactiveCocoa or 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 ReactiveObjC, check out the other documentation in this folder for a
framework overview and more in-depth information about how it all works in practice.
## New to ReactiveObjC?
ReactiveObjC 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 ReactiveObjC](#when-to-use-reactiveobjc)
1. [Framework Overview][]
1. [Basic Operators][]
1. [Header documentation](ReactiveObjC/)
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 this 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/ReactiveObjC/issues/new).
## Introduction
ReactiveObjC 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
没有合适的资源?快使用搜索试试~ 我知道了~
iOS AgreementView 简化版的隐私弹框(用户协议及隐私政策弹框)【包含超链接属性、demo支持中英文切换】
共644个文件
h:396个
m:137个
png:20个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
5星 · 超过95%的资源 2 下载量 20 浏览量
2023-07-12
14:09:50
上传
评论 2
收藏 704KB ZIP 举报
温馨提示
效果:https://img-blog.csdnimg.cn/9bc387378498453fb4a429dea355020d.png 1. 文章:https://blog.csdn.net/z929118967/article/details/126424314# 本文针对不熟悉iOS代码的读者,如果是有经验的开发请看这篇文章:https://kunnan.blog.csdn.net/article/details/103902362 2. 预备知识: - 采用富文本属性attributedText进行内容设置:https://blog.csdn.net/z929118967/article/details/107718162 - 本地化相关文章:https://blog.csdn.net/z929118967/article/details/125229417 - 适配相关文章:iOS15 UI适配之导航条主题: 背景颜色、标题颜色 :https://kunnan.blog.csdn.net/article/details/121090938 3. 使用MVVM架构
资源推荐
资源详情
资源评论
收起资源包目录
iOS AgreementView 简化版的隐私弹框(用户协议及隐私政策弹框)【包含超链接属性、demo支持中英文切换】 (644个子文件)
nosingle.css 992B
RACSignalProvider.d 222B
RACCompoundDisposableProvider.d 190B
.DS_Store 6KB
.DS_Store 6KB
RACSignal+Operations.h 33KB
RACmetamacros.h 30KB
RACSignal.h 23KB
RACSequence.h 18KB
RACStream.h 14KB
AXWebViewController.h 11KB
RACTuple.h 9KB
AXPracticalHUD.h 9KB
MASConstraint.h 8KB
RACScheduler.h 7KB
MASUtilities.h 6KB
AXSecurityPolicy.h 6KB
MASConstraintMaker.h 6KB
View+MASAdditions.h 5KB
RACCommand.h 5KB
View+MASShorthandAdditions.h 5KB
NSObject+RACPropertySubscribing.h 5KB
RACKVOChannel.h 5KB
RACEXTScope.h 4KB
ReactiveObjC.h 4KB
NSObject+RACSelectorSignal.h 4KB
Aspects.h 4KB
RACEXTRuntimeExtensions.h 3KB
RACChannel.h 3KB
QCTConsts.h 3KB
AXPopNavigationController.h 3KB
NSArray+MASAdditions.h 3KB
STModal.h 3KB
RACEXTKeyPathCoding.h 2KB
AXPracticalHUDContentView.h 2KB
RACSubscriptingAssignmentTrampoline.h 2KB
NSObject+RACLifting.h 2KB
NSObject+RACKVOWrapper.h 2KB
RACMulticastConnection.h 2KB
UIAlertView+RACSignalSupport.h 2KB
AXCircleProgressView.h 2KB
AXGradientProgressView.h 2KB
AXActivityIndicatorView.h 2KB
NSInvocation+RACTypeParsing.h 2KB
MASConstraint+Private.h 2KB
RACCompoundDisposable.h 2KB
RACEvent.h 2KB
AXBarProgressView.h 2KB
RACSubscriber.h 2KB
RACSerialDisposable.h 2KB
AXWebViewControllerActivity.h 2KB
AXBreachedAnnulusIndicatorView.h 2KB
HSSingleton.h 1KB
AXSpinningWaitCursor.h 1KB
AXIndicatorView.h 1KB
RACTestScheduler.h 1KB
RACQueueScheduler+Subclass.h 1KB
RACKVOProxy.h 1KB
MASViewConstraint.h 1KB
UIImagePickerController+RACSignalSupport.h 1KB
MASViewAttribute.h 1KB
NSDictionary+RACSequenceAdditions.h 1KB
UITextView+RACSignalSupport.h 1KB
UIActionSheet+RACSignalSupport.h 1KB
RACKVOTrampoline.h 1KB
UIControl+RACSignalSupportPrivate.h 1KB
AXPracticalHUDAnimator.h 1KB
RACScheduler+Private.h 1KB
NJKWebViewProgress.h 1KB
NSArray+MASShorthandAdditions.h 1016B
RACPassthroughSubscriber.h 1001B
NSString+RACKeyPathUtilities.h 989B
RACDisposable.h 988B
RACScheduler+Subclass.h 986B
QCTserviceAgreementView.h 965B
RACBlockTrampoline.h 946B
UITextField+RACSignalSupport.h 921B
NSURLConnection+RACSupport.h 893B
ViewController+MASAdditions.h 891B
RACDelegateProxy.h 878B
NSUserDefaults+RACSupport.h 864B
UITableViewHeaderFooterView+RACSignalSupport.h 851B
UICollectionReusableView+RACSignalSupport.h 847B
QCTSession.h 838B
UISegmentedControl+RACSignalSupport.h 817B
Masonry.h 802B
MKAnnotationView+RACSignalSupport.h 798B
NSObject+RACDeallocating.h 797B
UITableViewCell+RACSignalSupport.h 789B
RACTargetQueueScheduler.h 772B
RACSubject.h 749B
UIStepper+RACSignalSupport.h 733B
UISlider+RACSignalSupport.h 731B
UIDatePicker+RACSignalSupport.h 731B
RACReplaySubject.h 725B
UIRefreshControl+RACCommandSupport.h 717B
UIBarButtonItem+RACCommandSupport.h 709B
RACDynamicSequence.h 705B
QCTWebViewController.h 698B
NSString+RACSupport.h 690B
共 644 条
- 1
- 2
- 3
- 4
- 5
- 6
- 7
资源评论
- m0_752293832023-09-22资源是宝藏资源,实用也是真的实用,感谢大佬分享~
- gaolei1912232023-12-11资源使用价值高,内容详实,给了我很多新想法,感谢大佬分享~
iOS逆向
- 粉丝: 4974
- 资源: 104
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功