MOCollectionUtilities 1.0.1

MOCollectionUtilities 1.0.1

测试已测试
Lang语言 Obj-CObjective C
许可证 MIT
发布上次发布2014年12月

Maciej Oczko 维护。



描述

MOCollectionUtilities 是一组集合类公共方法,这些方法被分类在常见的基础集合类中。

特性

  • 突破性 isEmpty 方法
  • 多数组对象枚举
  • 数组和大字典操作
    • 映射
    • 筛选
    • 删除条目
    • 反转(仅限数组)
    • 为具有路径键的对象创建数组
  • 可变数组的栈模拟

所有操作都返回新的不可变对象。

基本用法

isEmpty

而不是

if ([array count] == 0) {
    // something
}

您可以使用完全突破性的方法和撰写

if([array isEmpty) {
   // something
}

它与大多数通用集合类兼容。

多数组枚举

如果您有许多具有相等对象数量的数组,您可以一次性枚举它们,例如:

[MOArraysEnumerator(array1, array2, array3) enumerateArraysUsingBlock:^(id obj1, id obj2, id obj3, NSUInteger idx, BOOL *stop) {
    // obj1 is array1[idx], obj2 is array2[idx], obj3 is array3[idx]
}];

您可以向 MOArraysEnumerator 宏中放入最多 5 个数组,自动完成将帮助您实现正确的方法(与 Appcode 一起易于使用)。

映射

数组

假设您有一个如下所示的数组

NSArray *array = @[
    @{
        @"number" : @1,
    },
    // and so on
];

您可以将此类数组映射到新的一个数组中,该数组只包含双倍的数字

NSArray *numbers = [array filter:^BOOL(NSDictionary *dictionary) {
    return @( [dictionary[@"number"] integerValue] * 2 );
}];

字典

假设您有一个如下所示的字典

NSDicationary *dictionary = @{
    @"number0" : @1,
    @"number1" : @2,
    // and so on
};

您可以将此字典映射到新的一个字典中

NSDictionary *differentDictionary = [dictionary map:^id(NSString *key, NSNumber *number) {
    NSUInteger keyLength = [key length];
    if (keyLength == 2) {
        return @( [number integerValue] * 2 );
    } else {
        return obj;
    }
}];

筛选

数组

您可以筛选数组

NSArray *differentArray = [array filter:^BOOL(NSDictionary *dictionary) {
    return [dictionary[@"number"] integerValue] % 2 == 0;
}];

字典

您可以筛选字典

NSDictionary *filteredDictionary = [dictionary filter:^BOOL(NSString *key, NSNumber *number) {
    return [key length] % 2 == 0;
}];

删除条目

数组

您可以从数组中轻松删除若干个对象

NSArray *arrayFixtures = [array without:@[
    obj1, obj2, obj3
]];

字典

您可以从字典中删除几个键(及其相关对象)

NSDictionary *selectedDictionary = [dictionary without:@[
     @"1", @"2", @"3"
]];

其他操作

反转

返回新数组实例,对象顺序反转

NSArray *reversedArray = [array reverse];

子对象数组创建

如果您想创建一个包含当前数组项目属性中保存的对象的数组,您可以编写

NSArray *numbers = [array arrayOfObjectsForKey:NSStringFromSelector(@selector(number))];

dictionaryWithValuesForKeysAsProperty

有点复杂。创建一个键是属性值,值是对象的字典。

NSString *propertyString = NSStringFromSelector(@selector(number));
NSDictionary *dictionary = [array dictionaryWithValuesForKeysAsProperty:propertyString];

dictionaryWithValuesForCustomKeys

与前一个操作非常相似,但您可以提供自定义键。

NSDictionary *dictionary = [array dictionaryWithValuesForCustomKeys:^id(NSDictionary *dictionary) {
    return [NSString stringWithFormat:@"%d", [dictionary[@"number"] integerValue] * 2];
}];

字典合并

非常简单且容易的方法,创建了一个由两个字典合并而成的新字典

NSDictionary *first = @{ ... };
NSDictionary *second = @{ ... };
NSDictionary *third = [first dictionaryWithObjectsAndKeysFromDictionary:second];

链式调用

所有提到的方法都可以链式调用,因为它们都返回新的不可变对象。因此,您可以编写如下内容

NSArray *brandNewArray = [[[[_fixtures map:^id(id object) {
    // map objects
    return mapping;
}] filter:^BOOL(id object) {
    // filter objects
    return filterCondition;
}] without:@[
    // list of unwanted objects
]] reverse];

栈式方法

您可以使用两种类型的栈:先进先出(FIFO)和后进先出(LIFO)。

NSMutableArray 上,您可以使用以下方法:

  • stack - 默认创建先进先出(FIFO)栈的简单工厂方法
  • stackWithType: - 创建 FIFO 或 LIFO 栈的工厂方法
  • push: - addObject: 的别名
  • peek - 根据栈类型(FIFO,LIFO)从数组的开始或结束返回
  • pop - 类似于 peek,但还会从栈中移除对象

安装

MOCollectionUtilities 通过 CocoaPods 提供。

包含头文件

#import <MOCollectionUtilities/MOCollectionUtilities.h>

Changelod

1.0.1

  • 为 NSSet 和 NSHashTable 添加了 map、filter 和 without 方法。

许可证

MOCollectionUtilities 根据 MIT 许可证提供。