最近在看一本书《Effective OC 2.0》,今天看到有个tip是OC适中循环各自优劣性,作者最终推荐此块循环。
阅读时思考了下块循环是否方便实现内部循环终止外部循环的问题。
于是做了如下验证,代码如下:
1 @H_403_10@// @H_403_10@ 2 @H_403_10@//@H_403_10@ main.m @H_403_10@ 3 @H_403_10@ 块循环最具优势! @H_403_10@ 4 @H_403_10@ 5 @H_403_10@ Created by LongMa on 2019/4/3. @H_403_10@ 6 @H_403_10@ Copyright © 2019年. All rights reserved. @H_403_10@ 7 @H_403_10@// @H_403_10@ 8 @H_403_10@ 9 #import <Foundation/Foundation.h> @H_403_10@10 @H_403_10@11 @H_403_10@int main(@H_403_10@int argc,@H_403_10@const @H_403_10@char *@H_403_10@ argv[]) { @H_403_10@12 @H_403_10@ @autoreleasepool { @H_403_10@13 NSMutableArray *lArr1 =@H_403_10@ [NSMutableArray array]; @H_403_10@14 NSMutableArray *lArr2 =@H_403_10@15 @H_403_10@for (@H_403_10@int i = 0; i < 10; ++@H_403_10@i) { @H_403_10@16 @H_403_10@ [lArr1 addObject:@(i)]; @H_403_10@17 @H_403_10@ [lArr2 addObject:@(i)]; @H_403_10@18 @H_403_10@ } @H_403_10@19 @H_403_10@20 @H_403_10@反向遍历 @H_403_10@21 @H_403_10@ [lArr1 enumerateObjectsWithOptions:(NSEnumerationReverse) @H_403_10@22 @H_403_10@ usingBlock:^( NSNumber* obj,NSUInteger idx,BOOL * _Nonnull stop) { @H_403_10@23 @H_403_10@ NSLog(@"%@,ind:%lu",obj,(unsigned long)idx); @H_403_10@24 @H_403_10@ if (idx == 90) { @H_403_10@25 @H_403_10@ *stop = YES; @H_403_10@26 @H_403_10@ } @H_403_10@27 @H_403_10@ }]; @H_403_10@28 @H_403_10@29 @H_403_10@30 @H_403_10@思考:里循环能否终止外循环? @H_403_10@31 @H_403_10@A:可以实现!需要差异化小标和停止参数,然后在里层循环对外循环的stopOut进行操作!此时,对内循环stopIn是否操作会决定内循环是否能执行完毕。 @H_403_10@32 @H_403_10@33 @H_403_10@/*@H_403_10@* 2019-04-03 01:08:30.003134+0800 块循环最具优势![3176:27943] 0,ind:0 @H_403_10@34 @H_403_10@ 2019-04-03 01:08:30.004040+0800 块循环最具优势![3176:27943] --0,1)">35 @H_403_10@ 2019-04-03 01:08:30.004143+0800 块循环最具优势![3176:27943] --1,ind:1 @H_403_10@36 @H_403_10@ 2019-04-03 01:08:30.004679+0800 块循环最具优势![3176:27943] --2,ind:2 @H_403_10@37 @H_403_10@ 2019-04-03 01:08:30.004756+0800 块循环最具优势![3176:27943] --3,ind:3 @H_403_10@38 @H_403_10@ 2019-04-03 01:08:30.004819+0800 块循环最具优势![3176:27943] --4,ind:4 @H_403_10@39 @H_403_10@ 2019-04-03 01:08:30.004881+0800 块循环最具优势![3176:27943] --5,ind:5 @H_403_10@40 @H_403_10@ 2019-04-03 01:08:30.004914+0800 块循环最具优势![3176:27943] --6,ind:6 @H_403_10@41 @H_403_10@ 2019-04-03 01:08:30.004945+0800 块循环最具优势![3176:27943] --7,ind:7 @H_403_10@42 @H_403_10@ 2019-04-03 01:08:30.005036+0800 块循环最具优势![3176:27943] --8,ind:8 @H_403_10@43 @H_403_10@ 2019-04-03 01:08:30.005072+0800 块循环最具优势![3176:27943] --9,ind:9 @H_403_10@*/ @H_403_10@44 [lArr1 enumerateObjectsUsingBlock:^( NSNumber* obj,NSUInteger idxOut,BOOL *@H_403_10@ _Nonnull stopOut) { @H_403_10@45 NSLog(@"%@,ind:%lu",(unsigned @H_403_10@long@H_403_10@)idxOut); @H_403_10@46 @H_403_10@47 @H_403_10@if (idxOut == 2@H_403_10@) { @H_403_10@48 *stopOut =@H_403_10@ YES; @H_403_10@49 @H_403_10@ } @H_403_10@50 @H_403_10@51 [lArr2 enumerateObjectsUsingBlock:^(@H_403_10@id _Nonnull obj,NSUInteger idxIn,1)"> _Nonnull stopIn) { @H_403_10@52 NSLog(--%@,1)">)idxIn); @H_403_10@53 @H_403_10@if (idxIn == 3@H_403_10@54 *stopOut =@H_403_10@55 @H_403_10@ } @H_403_10@56 @H_403_10@ }]; @H_403_10@57 @H_403_10@58 @H_403_10@59 @H_403_10@* 2019-04-03 01:05:20.987109+0800 块循环最具优势![3020:25596] 0,1)">60 @H_403_10@ 2019-04-03 01:05:20.987380+0800 块循环最具优势![3020:25596] --0,1)">61 @H_403_10@ 2019-04-03 01:05:20.987427+0800 块循环最具优势![3020:25596] --1,1)">62 @H_403_10@ 2019-04-03 01:05:20.987476+0800 块循环最具优势![3020:25596] --2,1)">63 @H_403_10@ 2019-04-03 01:05:20.987499+0800 块循环最具优势![3020:25596] --3,ind:3 @H_403_10@64 @H_403_10@ [lArr1 enumerateObjectsUsingBlock:^( NSNumber* obj,BOOL * _Nonnull stopOut) { @H_403_10@65 @H_403_10@ NSLog(@"%@,(unsigned long)idxOut); @H_403_10@66 @H_403_10@// @H_403_10@67 @H_403_10@ if (idxOut == 2) { @H_403_10@68 @H_403_10@ *stopOut = YES; @H_403_10@69 @H_403_10@70 @H_403_10@71 @H_403_10@72 @H_403_10@ [lArr2 enumerateObjectsUsingBlock:^(id _Nonnull obj,BOOL * _Nonnull stopIn) { @H_403_10@73 @H_403_10@ NSLog(@"--%@,(unsigned long)idxIn); @H_403_10@74 @H_403_10@ if (idxIn == 3) { @H_403_10@75 @H_403_10@ *stopIn = YES; @H_403_10@76 @H_403_10@ *stopOut = YES; @H_403_10@77 @H_403_10@78 @H_403_10@79 @H_403_10@80 @H_403_10@ }]; @H_403_10@81 @H_403_10@82 @H_403_10@83 @H_403_10@ } @H_403_10@84 @H_403_10@return 0@H_403_10@; @H_403_10@85 }
运行上面代码可见:
可以实现!需要差异化小标和停止参数,然后在里层循环对外循环的stopOut进行操作!此时,对内循环stopIn是否操作会决定内循环是否能执行完毕。原文链接:/iOS/883636.html