如何从 rac_sequence、反应式 cocoa、ios 获取项目
how to get item from rac_sequence, reactive cocoa, ios
我正在解析服务器响应 header 中所需的链接。我的 header 看起来像
Access-Control-Allow-Origin → * Age → 0 Cache-Control →
private,must-revalidate
Connection → keep-alive
Content-Encoding → gzip Content-Type → application/json
Date → Sat, 13 Jun 2015 15:58:56 GMT ETag →
W/"cb38bb07f1635fd6aba5969985bf0607"
Link → http://thisIsCurrentlink&limit=24;
rel="next",http://thisIsLastlink&limit=24;
rel="last",http://thisIsFirstlink&limit=24; rel="first",<>;
rel="prev"
Server → nginx
Vary → Accept-Encoding
X-Total-Count → 131
transfer-encoding → chunked
通过这样做,我可以获得包含所有链接的 links
数组
NSString *linkHeader = [(NSHTTPURLResponse *)operation.response allHeaderFields][@"Link"];
NSArray *links = [linkHeader componentsSeparatedByString:@","];
然后我将执行以下操作以获取我需要的所有链接
RACSequence *sequence = [links.rac_sequence map:^id(NSString* item) {
NSError *error;
NSLog(@"item is %@",item);
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<(.*)>" options:0 error:&error];
NSString *actualLink;
if (regex != nil) {
NSTextCheckingResult *result = [regex firstMatchInString:item
options:0
range:NSMakeRange(0, item.length)
];
if (result) {
NSRange range = [result rangeAtIndex:1];
actualLink = [item substringWithRange:range];
NSLog(@"actualLink is %@",actualLink);
return [RACSequence return:actualLink];
}
}
return [RACSequence empty];
}];
现在 sequence
包含所有
的链接
http://thisIsCurrentlink&limit=24
http://thisIsLastlink&limit=24
http://thisIsFirsttlink&limit=24
我的问题是如何从上方访问每个项目sequence
只需使用array
方法,将RACSequence转换为数组。
此外,您不需要使用 RACSequence return:
,那是在您的序列中创建序列,只需 return 通常的字符串和 nil,然后在 map
在尝试将其提取为数组之前删除所有 nil 值。
示例:
NSArray *actualLinks = [[links.rac_sequence map:^id(NSString* item) {
NSError *error = nil;
NSLog(@"item is %@",item);
NSRegularExpression *regex =
[NSRegularExpression regularExpressionWithPattern:@"<(.*)>" options:0 error:&error];
NSString *actualLink = nil;
if (regex != nil) {
NSTextCheckingResult *result = [regex firstMatchInString:item
options:0
range:NSMakeRange(0, item.length)];
if (result) {
NSRange range = [result rangeAtIndex:1];
actualLink = [item substringWithRange:range];
NSLog(@"actualLink is %@",actualLink);
}
}
return actualLink;
}] ignore:nil].array;
我正在解析服务器响应 header 中所需的链接。我的 header 看起来像
Access-Control-Allow-Origin → * Age → 0 Cache-Control → private,must-revalidate
Connection → keep-alive
Content-Encoding → gzip Content-Type → application/json
Date → Sat, 13 Jun 2015 15:58:56 GMT ETag → W/"cb38bb07f1635fd6aba5969985bf0607"
Link → http://thisIsCurrentlink&limit=24; rel="next",http://thisIsLastlink&limit=24; rel="last",http://thisIsFirstlink&limit=24; rel="first",<>; rel="prev"
Server → nginx
Vary → Accept-Encoding
X-Total-Count → 131
transfer-encoding → chunked
通过这样做,我可以获得包含所有链接的 links
数组
NSString *linkHeader = [(NSHTTPURLResponse *)operation.response allHeaderFields][@"Link"];
NSArray *links = [linkHeader componentsSeparatedByString:@","];
然后我将执行以下操作以获取我需要的所有链接
RACSequence *sequence = [links.rac_sequence map:^id(NSString* item) {
NSError *error;
NSLog(@"item is %@",item);
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<(.*)>" options:0 error:&error];
NSString *actualLink;
if (regex != nil) {
NSTextCheckingResult *result = [regex firstMatchInString:item
options:0
range:NSMakeRange(0, item.length)
];
if (result) {
NSRange range = [result rangeAtIndex:1];
actualLink = [item substringWithRange:range];
NSLog(@"actualLink is %@",actualLink);
return [RACSequence return:actualLink];
}
}
return [RACSequence empty];
}];
现在 sequence
包含所有
http://thisIsCurrentlink&limit=24
http://thisIsLastlink&limit=24
http://thisIsFirsttlink&limit=24
我的问题是如何从上方访问每个项目sequence
只需使用array
方法,将RACSequence转换为数组。
此外,您不需要使用 RACSequence return:
,那是在您的序列中创建序列,只需 return 通常的字符串和 nil,然后在 map
在尝试将其提取为数组之前删除所有 nil 值。
示例:
NSArray *actualLinks = [[links.rac_sequence map:^id(NSString* item) {
NSError *error = nil;
NSLog(@"item is %@",item);
NSRegularExpression *regex =
[NSRegularExpression regularExpressionWithPattern:@"<(.*)>" options:0 error:&error];
NSString *actualLink = nil;
if (regex != nil) {
NSTextCheckingResult *result = [regex firstMatchInString:item
options:0
range:NSMakeRange(0, item.length)];
if (result) {
NSRange range = [result rangeAtIndex:1];
actualLink = [item substringWithRange:range];
NSLog(@"actualLink is %@",actualLink);
}
}
return actualLink;
}] ignore:nil].array;