GithubHelp home page GithubHelp logo

testractuplesequence's Introduction

RACTupleSequence作为RACSequence的子类,顾名思义,就是对元祖的操作,根据新增加的方法sequenceWithTupleBackingArray:offset:可知,该类是对元祖的数组对象做处理。

完整测试用例在这里

.m中的方法如下:

+ (instancetype)sequenceWithTupleBackingArray:(NSArray *)backingArray offset:(NSUInteger)offset {
	NSCParameterAssert(offset <= backingArray.count);

	if (offset == backingArray.count) return self.empty;

	RACTupleSequence *seq = [[self alloc] init];
	seq->_tupleBackingArray = backingArray;
	seq->_offset = offset;
	return seq;
}

创建RACTupleSequence对象,保存参数值backingArray offset

测试用例:

- (void)test_sequenceWithTupleBackingArray
{
    RACTuple *tuple = RACTuplePack(@1, @2, @3);
    RACSequence *sequence = [RACTupleSequence sequenceWithTupleBackingArray:tuple.allObjects offset:1];
    NSLog(@"sequenceWithTupleBackingArray -- %@", sequence);
    
    // 打印日志;
    /*
     2018-08-17 17:40:07.673830+0800 TestRACTupleSequence[52426:18393668] sequenceWithTupleBackingArray -- <RACTupleSequence: 0x600000236600>{ name = , tuple = (
     1,
     2,
     3
     ) }
     */
}

- (id)head {
	id object = self.tupleBackingArray[self.offset];
	return (object == RACTupleNil.tupleNil ? NSNull.null : object);
}

获取tupleBackingArray指定偏移量offset下的值作为head值。

测试用例:

- (void)test_head
{
    RACTuple *tuple = RACTuplePack(@1, @2, @3);
    RACSequence *sequence = [RACTupleSequence sequenceWithTupleBackingArray:tuple.allObjects offset:1];
    NSLog(@"head -- %@", sequence.head);
    
    // 打印日志;
    /*
     2018-08-17 17:41:34.131249+0800 TestRACTupleSequence[52496:18398390] head -- 2
     */
}

- (RACSequence *)tail {
	RACSequence *sequence = [self.class sequenceWithTupleBackingArray:self.tupleBackingArray offset:self.offset + 1];
	sequence.name = self.name;
	return sequence;
}

通过sequenceWithTupleBackingArray:offset:并将偏移量增加1生成一个RACSequence对象作为tail值。

测试用例:

- (void)test_tail
{
    RACTuple *tuple = RACTuplePack(@1, @2, @3);
    RACSequence *sequence = [RACTupleSequence sequenceWithTupleBackingArray:tuple.allObjects offset:1];
    NSLog(@"tail -- %@", sequence.tail);
    
    // 打印日志;
    /*
     2018-08-17 17:42:32.102848+0800 TestRACTupleSequence[52540:18401646] tail -- <RACTupleSequence: 0x60000022c300>{ name = , tuple = (
     1,
     2,
     3
     ) }
     */
}

- (NSArray *)array {
	NSRange range = NSMakeRange(self.offset, self.tupleBackingArray.count - self.offset);
	NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:range.length];

	[self.tupleBackingArray enumerateObjectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:range] options:0 usingBlock:^(id object, NSUInteger index, BOOL *stop) {
		id mappedObject = (object == RACTupleNil.tupleNil ? NSNull.null : object);
		[array addObject:mappedObject];
	}];

	return array;
}

通过tupleBackingArray调用enumerateObjectsAtIndexes:方法获取数组从指定偏移量offset开始的所有数据,组成一个新的数组返回。

测试用例:

- (void)test_array
{
    RACTuple *tuple = RACTuplePack(@1, @2, @3);
    RACSequence *sequence = [RACTupleSequence sequenceWithTupleBackingArray:tuple.allObjects offset:1];
    NSLog(@"array -- %@", sequence.array);
    
    // 打印日志;
    /*
     2018-08-17 17:43:20.283402+0800 TestRACTupleSequence[52589:18404640] array -- (
     2,
     3
     )
     */
}

所以该类的作用就是将元祖的数组转换成一个序列,通过序列的方法获取元祖的值。其实跟RACArraySequence的方法非常相似,只是RACArraySequence获取的值并不会做处理。而RACTupleSequence会将RACTupleNil类型转换为NSNull类型,如果对RACTuple有所了解,就会知道RACTuple某种情况下会将NSNull转为RACTupleNil,这里的操作刚好是还原原始数据。

如果对RACTuple不了解,请看这里

testractuplesequence's People

Contributors

jianghui1 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.