测试已测试 | ✓ |
语言语言 | Obj-CObjective C |
许可证 | MIT |
发布上次发布 | 2014年12月 |
由 Daniel L. Alves 维护。
NitroNSArrayCategories 提供了一些 NSArray
缺少的特性和工具。您将找到:
1) 转换方法
对每个数组对象执行操作,返回结果的新数组。
NSArray *incredibles = @[ @"Mr. Incredible", @"Elastigirl", @"Violet", @"Violet", @"Jack-Jack" ];
// With selectors that receive no arguments:
// uppercasedIncredibles will be @[ @"MR. INCREDIBLE", @"ELASTIGIRL", @"VIOLET", @"DASH", @"JACK-JACK"];
NSArray *uppercasedIncredibles = [incredibles transformWithSelector: @selector(uppercaseString)];
// ...
NSArray *animals = @[ @{ @"kind" : @"cat", @"id": @( 1 ) },
@{ @"kind" : @"dog", @"id": @( 2 ) },
@{ @"kind" : @"pig", @"id": @( 3 ) } ];
// With selectors that receive one argument:
// animalKinds will be @[ @"cat", @"dog", @"pig" ]
NSArray *animalKinds = [animals transformWithSelector: @selector(objectForKeyedSubscript:)
andObject: @"kind"];
// With blocks:
// animalsIds will be @[ @1, @2, @3 ]
NSArray *animalIds = [animals transformWithBlock: ^NSObject * ( NSObject *obj ){
return (( NSDictionary * )obj)[ @"id"];
}];
2) 安全索引方法
如果索引超出范围,则返回 nil
(或 NSNull
)
NSArray *array = @[ @1, @2, @3 ];
[array nilOrObjectAtIndex: 2]; // Returns @3
[array nilOrObjectAtIndex: 3]; // Returns nil
[array nsnullOrObjectAtIndex: 90]; // Returns [NSNull null]
3) 筛选方法
厌倦了调用 indexesOfObjectsPassingTest:
然后调用 objectsAtIndexes:
?这里就是了
NSArray *array = @[ @0, @1, @2, @3, @4, @5, @6, @7, @8, @9 ];
// oddNumbers will be @[ @1, @3, @5, @7, @9 ];
NSArray *oddNumbers = [array objectsPassingTest: ^BOOL( id obj, NSUInteger idx, BOOL *stop ){
return ([( NSNumber * )obj intValue] & 1 ) != 0;
}];
iOS 4.3 或更高版本,仅限于 ARC
NitroNSArrayCategories 可通过 CocoaPods 获取,要安装它,请将以下行添加到您的 Podfile:
pod "NitroNSArrayCategories"
NitroNSArrayCategories 向使用它的目标添加了 -ObjC
链接器标志。没有它,类别代码将被删除,从而导致链接器错误。有关静态库中类别的更多信息,请参阅:构建带有类别的 Objective-C 静态库
NitroNSArrayCategories 在 MIT 许可证下提供。有关更多信息,请参阅 LICENSE 文件。