TestsTested | ✓ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Dec 2014 |
Maintained by Maciej Oczko.
MOCollectionUtilities is a set of useful method gathered in categories for common Foundation collection classes.
All operations return new immutable object.
Instead of :
if ([array count] == 0) {
// something
}
You can use completely breakthrough method and write:
if([array isEmpty) {
// something
}
It works with most common collection classes.
If you have many arrays with equal count of objects, you can enumerate them all at once, e.g.:
[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]
}];
You can put into MOArraysEnumerator
macro up to 5 arrays and autocompletion will help you to implement proper method (easy use with Appcode).
Let's assume you have an array like:
NSArray *array = @[
@{
@"number" : @1,
},
// and so on
];
You can map such array into a new one having only doubled numbers:
NSArray *numbers = [array filter:^BOOL(NSDictionary *dictionary) {
return @( [dictionary[@"number"] integerValue] * 2 );
}];
Let's assume you have a dictionary like:
NSDicationary *dictionary = @{
@"number0" : @1,
@"number1" : @2,
// and so on
};
You can map such dictionary into a new one:
NSDictionary *differentDictionary = [dictionary map:^id(NSString *key, NSNumber *number) {
NSUInteger keyLength = [key length];
if (keyLength == 2) {
return @( [number integerValue] * 2 );
} else {
return obj;
}
}];
You can filter array:
NSArray *differentArray = [array filter:^BOOL(NSDictionary *dictionary) {
return [dictionary[@"number"] integerValue] % 2 == 0;
}];
You can filter dictionary:
NSDictionary *filteredDictionary = [dictionary filter:^BOOL(NSString *key, NSNumber *number) {
return [key length] % 2 == 0;
}];
You can remove a couple of object from array easy:
NSArray *arrayFixtures = [array without:@[
obj1, obj2, obj3
]];
You can remove a few keys (with related objects) from dictionary:
NSDictionary *selectedDictionary = [dictionary without:@[
@"1", @"2", @"3"
]];
Return new array instance with objects in reversed order:
NSArray *reversedArray = [array reverse];
If you'd like to make an array of objects which are held in a property of current array items, you can write:
NSArray *numbers = [array arrayOfObjectsForKey:NSStringFromSelector(@selector(number))];
Tricky one. Create a dictionary where the key is the property and the value is the object.
NSString *propertyString = NSStringFromSelector(@selector(number));
NSDictionary *dictionary = [array dictionaryWithValuesForKeysAsProperty:propertyString];
Very similar as previous operation, but you can provide custom key.
NSDictionary *dictionary = [array dictionaryWithValuesForCustomKeys:^id(NSDictionary *dictionary) {
return [NSString stringWithFormat:@"%d", [dictionary[@"number"] integerValue] * 2];
}];
Very simple and easy method that creates new dictionary which is an union of two:
NSDictionary *first = @{ ... };
NSDictionary *second = @{ ... };
NSDictionary *third = [first dictionaryWithObjectsAndKeysFromDictionary:second];
It's possible to chain all mentioned methods, because each of them return new, immutable object. So you can write something like that:
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];
You can use two types of stack: FIFO and LIFO.
On NSMutableArray
you can use following methods:
stack
- simple factory method which creates FIFO stack by defaultstackWithType:
- factory method which creates FIFO or LIFO stackpush:
- alias for addObject:
peek
- returns from the beginning or from the end of array, depending on stack type (FIFO, LIFO) pop
- like peek
but additionally it removes object from stackMOCollectionUtilities is available through CocoaPods.
#import <MOCollectionUtilities/MOCollectionUtilities.h>
MOCollectionUtilities is available under the MIT license.