iOS面试资料iOS 开发技巧iOS小知识

iOS程序员手写这段代码,当场被聘用

2018-03-24  本文已影响1639人  iOS雯Ping

为什么使用RAC?

因为RAC具有高聚合低耦合的思想所以使用RAC会让代码更简洁,逻辑更清晰。

如何在项目中添加RAC?

方法:可以使用Cocoapods导入RAC

具体怎么使用:

创建一个新项目,使用Cocoapods在项目中导入该框架。将下面的代码复制到Podfile文件然后更新


建pch文件,在pch文件中导入该框架。

利用这个框架实现下面需求,来具体说明他该如何使用。

两个输入框内都有内容时,下面的按钮是红色。

两个输入框内都没内容时,下面的按钮是灰色。

将下面代码粘贴到项目中的ViewController.m中。

运行显示如下图:

工作原理:


常见类解释:

1. Stream - 信号流值 - RACStream类

表示一个基本单元可以为任意值,其值会随着事件的变化而变化,可以在其上进行一些复杂的操作运算(map,filter,skip,take等.)此类不会被经常使用, 多情况下表现为signal和sequences(RACSignal 和RACSequence继承于RACStream类)

2. Signals - 信号 - RACSignal类

ReactiveCocoa是什么

在使用ReactiveCocoa 过程中,除去RACSignal和RACSubject这些信号类以外,有些时候我们可能还需要封装一些固定的操作集合。这些操作集合都是固定的,每次只要一触发就会执行事先定义好的一个过程。在iOS开发过程中,按钮的点击事件就可能有这种需求。那么RACCommand就可以实现这种需求。

当然除了封装一个操作集合以外,RACCommand还能集中处理错误等等功能。今天就来从底层来看看RACCommand是如何实现的。

1.RACCommand的定义

@interfaceRACCommand:NSObject@property(nonatomic,strong,readonly) RACSignal *executionSignals;@property(nonatomic,strong,readonly) RACSignal *executing;@property(nonatomic,strong,readonly) RACSignal *enabled;@property(nonatomic,strong,readonly) RACSignal *errors;@property(atomic,assign)BOOLallowsConcurrentExecution;volatileuint32_t _allowsConcurrentExecution;@property(atomic,copy,readonly)NSArray*activeExecutionSignals;NSMutableArray*_activeExecutionSignals;@property(nonatomic,strong,readonly) RACSignal *immediateEnabled;@property(nonatomic,copy,readonly) RACSignal * (^signalBlock)(idinput);@end

RACCommand中4个最重要的信号就是定义开头的那4个信号,executionSignals,executing,enabled,errors。需要注意的是,这4个信号基本都是(并不是完全是)在主线程上执行的

1. RACSignal *executionSignals

executionSignals是一个高阶信号,所以在使用的时候需要进行降阶操作,降价操作在前面分析过了,在ReactiveCocoa v2.5中只支持3种降阶方式,flatten,switchToLatest,concat。降阶的方式就根据需求来选取。

还有选择原则是,如果在不允许Concurrent并发的RACCommand中一般使用switchToLatest。如果在允许Concurrent并发的RACCommand中一般使用flatten。

2. RACSignal *executing

executing这个信号就表示了当前RACCommand是否在执行,信号里面的值都是BOOL类型的。YES表示的是RACCommand正在执行过程中,命名也说明的是正在进行时ing。NO表示的是RACCommand没有被执行或者已经执行结束。

3. RACSignal *enabled

enabled信号就是一个开关,RACCommand是否可用。这个信号除去以下2种情况会返回NO:

RACCommand 初始化传入的enabledSignal信号,如果返回NO,那么enabled信号就返回NO。

RACCommand开始执行中,allowsConcurrentExecution为NO,那么enabled信号就返回NO。

除去以上2种情况以外,enabled信号基本都是返回YES。

4. RACSignal *errors

errors信号就是RACCommand执行过程中产生的错误信号。这里特别需要注意的是:在对RACCommand进行错误处理的时候,我们不应该使用subscribeError:对RACCommand的executionSignals


//

//  _03__RACTests.m

//  003--RACTests

//

//  Created by CC老师 on 2017/10/19.

//  Copyright© 2017年 CC老师. All rights reserved.

//

#import

@interface _03__RACTests : XCTestCase

@end

@implementation _03__RACTests

- (void)setUp {

    [super setUp];

    // Put setup code here. This method is called before the invocation of each test method in the class.

}

- (void)tearDown {

    // Put teardown code here. This method is called after the invocation of each test method in the class.

    [super tearDown];

}

- (void)testExample {

    // This is an example of a functional test case.

    // Use XCTAssert and related functions to verify your tests produce the correct results.

}

- (void)testPerformanceExample {

    // This is an example of a performance test case.

    [self measureBlock:^{

        // Put the code you want to measure the time of here.

    }];

}

@end

Created by CC老师 on 2017/10/19.

//  Copyright© 2017年 CC老师. All rights reserved.

//

#import

//  _03__RACUITests.m

//  003--RACUITests

//

//  st/XCTest.h>

@interface _03__RACUITests : XCTestCase

@end

@implementation _03__RACUITests

- (void)setUp {

    [super setUp];

    // Put setup code here. This method is called before the invocation of each test method in the class.

    // In UI tests it is usually best to stop immediately when a failure occurs.

    self.continueAfterFailure = NO;

    // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.

    [[[XCUIApplication alloc] init] launch];

    // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.

}

- (void)tearDown {

    // Put teardown code here. This method is called after the invocation of each test method in the class.

    [super tearDown];

}

- (void)testExample {

    // Use recording to get started writing UI tests.

    // Use XCTAssert and related functions to verify your tests produce the correct results.

}

@end

今天给大家的分享就到这吧!有收获,或者喜欢小编的可以关注加小编的微信同时也欢迎大家加入或小编的iOS交流群687528266,点击链接加入群聊【iOS软件开发学习交流】:https://jq.qq.com/?_wv=1027&k=53OMouY,相互学习!大家一起交流成长!!

上一篇下一篇

猜你喜欢

热点阅读