Sometimes, Objective-C is just overly verbose. Life is too short to enumerateObjectsUsingBlock and who has the time to create sub-arrays with filteredArrayUsingPredicate anyway?

OpinionatedC is here to fix that. It offers a ton of Smalltalk-style convenience extension methods that make writing concise, easily readable Objective-C code a pleasure.

Usage

The easiest way to include OpinionatedC into your project is through CocoaPods:

pod 'OpinionatedC'

Import the umbrella header everywhere you want to taste the sweetness of OpinionatedC:

#import <OpinionatedC/OpinionatedC.h>

Features

Collection Extensions

Currently, the collection extensions offered by OpinionatedC are available on all instances of the following classes: NSArray, NSMutableArray, NSDictionary, NSMutableDictionary, NSSet, NSMutableSet, NSString, NSMutableString, NSMapTable, NSHashTable, NSPointerArray.

The collection extensions are implemented on the NSObject level, with refined behaviour for the different collection types. Non-collection objects thereby behave like collections with a single element, and [NSNull null] behaves like an empty collection.

OpinionatedC tries it's best to preserve the types you are operating on. Calling select: on an (immutable) NSArray will yield an instance of NSArray. Likewise, doing the same on an NSMutableArray instance will yield a mutable array. However, this behaviour is limited by the type system of Objective-C (for instance, this does not work at all with NSString and NSMutableString).

Aggregation

Average (average / average:)

[@[@2, @4] average]
// => @3

[@[@"hello", @"world!"] average:^NSNumber*(id each) { 
    return @([each length]);
}]
// => @5.5

Count (count:)

[@[@"a", @5, @YES, @"b"] count:^BOOL(id each) {
    return [each isKindOfClass:NSString.class];
}]
// => 2

Group By (groupedBy:)

NSSet *set = [NSSet setWithObjects:@"foo", @"bar", @"hello", @"world!", nil];
[set groupedBy:^id(id each) {
    return @([each length]);
}]
// => @{
//        @3 : a NSSet(@"foo", @"bar"),
//        @5 : a NSSet(@"hello"),
//        @6 : a NSSet(@"world!")
//    }

Max (max / max:)

[[@1, @2, @3] max]
// => @3

[@[@"hello", @"world!"] max:^NSNumber*(id each) { 
    return @([each length]);
}]
// => @"world!"

Min (min / min:)

[@[@1, @2, @3] min]
// => @1

[@[@"hello", @"world!"] min:^NSNumber*(id each) { 
    return @([each length]);
}]
// => @"hello"

Sum (sum / sum:)

[@[@1, @2, @3] sum]
// => @6

[@[@"hello", @"world!"] sum:^NSNumber*(id each) { 
    return @([each length]);
}]
// => @11

Enumeration

Each (each: / eachWithIndex: / each:separatedBy: / eachWithIndex:separatedBy:)

[@[@"foo", @"bar"] each:^(NSString *each) {
    NSLog(@"%@", each);
}]
// => foo
// => bar

[@[@"foo", @"bar"] 
    each:^(NSString *each) {
        NSLog(@"%@", each);
    }
    separatedBy:^{
        NSLog(@"w00t");
    }]
// => foo
// => w00t
// => bar

[@"abc" eachWithIndex:^(NSString *each, NSUInteger idx) {
    NSLog(@"%@ - %@", each, @(idx));
}]
// => a - 0
// => b - 1
// => c - 2

Is Empty (isEmpty / isNotEmpty)

[@[@"a", @"b", @"c"] isEmpty]
// => NO

[@"" isEmpty]
// => YES

[@"foo" isEmpty]
// => NO

Map / Reduce

Map (map: / collect:)

[@[@1, @2, @3] map:^id(NSNumber *each) {
    return @([each integerValue] * 2);
}]
// => @[@2, @4, @6]

[@"hello world" collect:^id(NSString *each) { 
    return [each capitalizedString];
}]
// => @"HELLO WORLD"

[@{ @1 : @YES } map:^id(OCAssociation *each) { 
    return [each.key asAssociationWithValue:@NO];
}]
// => @{ @1 : @NO }

Reduce (inject:into: / reduce:)

[@[@1, @2, @3] inject:@0 into:^id(NSNumber *running, NSNumber *each) {
    return @([running integerValue] + [each integerValue]);
}]
// => @6

[@[@4, @5, @6] reduce:^id(NSNumber *running, NSNumber *each) {
    return @([running integerValue] * [each integerValue]);
}]
// => @120

Sub-Collections

All & Any Satisfy (allSatisfy: / anySatisfy:)

[@[@1, @2, @3, @4] allSatisfy:^BOOL(NSNumber *each) {
    return [each integerValue] % 2 == 0;
}]
// => NO

[@"abcdef" anySatisfy:^BOOL(NSString *each) { 
    return [each isEqualToString:@"f"];
}]
// => YES

First (first / first:)

[@[@5, @6] first]
// => @5

[@"abcdef" first:3]
// => @"abc"

Detect (detect:)

[@[@2, @3, @4] detect:^BOOL(NSNumber *first) {
    return [each integerValue] % 2 == 0;
}]
// => @2

[@{ @1 : @"foo", @2 : @"bar"} detect:^BOOL(OCAssociation *first) {
    return [first.key isEqualToNumber:@2];
}]
// => an OCAssociation(@2, @"bar")

Select & Reject (select: / reject:)

[@{ @1 : @"foo", @2 : @"bar"} select:^BOOL(OCAssociation *each) {
    return [each.key isEqualToNumber:@2];
}]
// => @{ @2 : @"bar" }

[@{ @1 : @"foo", @2 : @"bar"} reject:^BOOL(OCAssociation *each) {
    return [each.key isEqualToNumber:@2];
}]
// => @{ @1 : @"foo" }

Drop & Take While (dropWhile: / takeWhile:)

[@[@2, @4, @6, @8, @10, @11, @12] dropWhile:^BOOL(id each) {
    return [each integerValue] % 2 == 0;
}]
// => @[@11, @12]

[@[@1, @2, @3, @4, @5] takeWhile:^BOOL(id each) {
    return [each integerValue] <= 3;
}]
// => @[@1, @2, @3]

Error Handling

@implementation MyAbstractClass

- (NSString *)abstractMethod {
    SubclassResponsibility;
}

- (BOOL)methodThatShouldBeImplemented {
    NotYetImplemented;
}

- (void)unsupportedMethodFromSuperclass {
    UnsupportedOperation;
}

- (NSNumber *)methodWithArg:(NSUInteger)arg {

    switch (arg) {
        case 1: return @1;
        case 2: return @2;
    }

    ShouldNotOccur;

}

- (NSUInteger)method2WithArg:(NSUInteger)arg {
    if (arg < 10) {
        return 10;
    }
    if (arg < 20) {
        return 20;
    }
    Error(@"arg must be smaller than 20");
}

@end

NSDictionary Extensions

NSMutableDictionary *dict = [NSMutableDictionary dictionary]

[dict add:[@1 asAssociationWithValue:@"foo"]]
// => @{ @1 : @"foo" }

[dict at:@1 put:@"foo"]
// => @{ @1 : @"foo" }

[dict at:@2 ifAbsent:^id{
    return @"bar";
}]
// => @"bar"

[dict at:@1 ifPresent:^id(id element) {
    return @"bar";
}]
// => @"bar"

[dict at:@2 ifAbsentPut:^id{
    return @"hello world";
}]
// => @"hello world"

[dict includesKey:@2]
// => YES

[dict includesValue:@"bar"]
// => NO

for (OCAssociation *each in [dict associationEnumerator]) {
    NSLog(@"%@", each.value);
}
// => foo

NSNumber Extensions

Random Number Generation (atRandom)

[@100 atRandom]
// => @77

Repetition (timesRepeat: / timesRepeatWithIndex:)

[@3 timesRepeat:^{ 
   NSLog(@"hooray!"); 
}]
// => hooray
// => hooray
// => hooray

__block NSMutableArray *array = [NSMutableArray array];
[@10 timesRepeatWithIndex:^(NSUInteger idx) {
    [array addObject:@(idx)];
}]
// => @[@0, @1, @2, @3, @4, @5, @6, @7, @8, @9]

Intervals (to: / to:by:)

[[@5 to:@7] map:^id(NSNumber *each) {
    return @([each integerValue] * 2);
}]
// => @[@10, @12, @14]

[[@1 to:@10 by:@1] select:^id(NSNumber *each) {
    return [each integerValue] % 2 == 0;
}]
// => @[@2, @4, @6, @8, @10]

NSObject Extensions

[@"hello world" asAssociationWithKey:@1]
// => an OCAssociation(key:@1, value:@"hello world")

[@5 isNull]
// => NO

[[NSNull null] isNotNull]
// => NO

License

The MIT License (MIT)

Copyright (c) 2015 Leo Schweizer

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.