Last active
December 14, 2016 09:26
-
-
Save KeithMorning/b254497e62d492605827c79c5dc4d6ad to your computer and use it in GitHub Desktop.
给你一个嵌套的 NSArray 数据,实现一个迭代器类,该类提供一个 next() 方法,可以依次的取出这个 NSArray 中的数据。 比如 NSArray 如果是 [1,[4,3],6,[5,[1,0]]], 则最终应该输出:1, 4, 3, 6, 5, 1, 0 。 另外,实现一个 allObjects 方法,可以一次性取出所有元素。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// arrayIterator.m | |
// arrayIterator | |
// | |
// Created by keith xi on 12/14/16. | |
// Copyright © 2016 HelloBaby. All rights reserved. | |
// | |
#import "arrayIterator.h" | |
@implementation arrayIterator | |
- (NSArray *)allObjects{ | |
NSMutableArray *result = [NSMutableArray new]; | |
[self orgianlArray:self.OrginalArray result:result]; | |
return [result copy]; | |
} | |
- (void)orgianlArray:(NSArray *)array result:(NSMutableArray *)result{ | |
if (!array.count) { | |
return; | |
} | |
for (id obj in array) { | |
if ([obj isKindOfClass:[NSArray class]]) { | |
[self orgianlArray:obj result:result]; | |
}else{ | |
[result addObject:obj]; | |
} | |
} | |
} | |
- (id(^)())iterator{ | |
NSArray *flattarry = [self allObjects]; | |
return [self iterator:flattarry]; | |
} | |
//如果允许调用第二题方法,只需要一个迭代器就好了 | |
- (id(^)())iteratorWithFlatArray:(NSArray *)orignalArray{ | |
if (!orignalArray.count) { | |
return nil; | |
} | |
__block NSInteger i = 0; | |
id(^func)() = ^id(){ | |
if (i< orignalArray.count) { | |
id obj =[orignalArray objectAtIndex:i]; | |
i++; | |
return obj; | |
} | |
return nil; | |
}; | |
return func; | |
} | |
//好吧,可能会被作者打死 | |
//思路 1 取array第一个元素 2放入队列 3 取出判断是否为 NSArray 是->展开,不是直接取出从队列移除 4判断queue中有没值 5->判断是否array取完,没有->添加到queue | |
- (id(^)())iterator:(NSArray *)array{ | |
if (!array.count) { | |
return nil; | |
} | |
__block NSInteger i =0; | |
__block NSMutableArray *quque = [NSMutableArray new]; | |
[quque addObject:[array objectAtIndex:i]]; | |
id(^iteratorFuc)() = ^id(){ | |
id obj = quque.lastObject; | |
while ([obj isKindOfClass:[NSArray class]]) { | |
[quque removeObject:obj]; | |
[quque addObjectsFromArray:obj]; | |
obj = quque.lastObject; | |
} | |
[quque removeObject:obj]; | |
if (quque.count == 0) { | |
i++; | |
if (i<array.count) { | |
[quque addObject:[array objectAtIndex:i]]; | |
} | |
} | |
return obj; | |
}; | |
return iteratorFuc; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// arrayIterator.h | |
// arrayIterator | |
// | |
// Created by keith xi on 12/14/16. | |
// Copyright © 2016 HelloBaby. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
@interface arrayIterator : NSObject | |
@property (nonatomic,strong) NSArray *OrginalArray; | |
- (NSArray *)allObjects; | |
- (id(^)())iterator; | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment