Quantcast
Channel: Envato Tuts+ Code - Mobile Development
Viewing all articles
Browse latest Browse all 1836

Having Fun with YOLOKit

$
0
0

Enumerating collections in Objective-C is often verbose and clunky. If you're used to Ruby or worked with Underscore or Lo-Dash in JavaScript, then you know there're more elegant solutions. That is exactly what the creators of YOLOKit thought when they created this nifty library. YOLOKit's tagline is Enumerate Foundation delightfully and they mean it.

1. Installation

Adding YOLOKit to an Xcode project is very easy with CocoaPods. Include the pod in your project's Podfile, run pod update from the command line, and import YOLO.h wherever you want to use YOLOKit.

If you're not using CocoaPods, then download the library from GitHub, add the relevant files to your project, and import YOLOKit's header.

2. Using YOLOKit

YOLOKit has a lot to offer, but in this quick tip I'll only focus on a few of the methods YOLOKit has in its repertoire.

Minimum and Maximum

Let's start simple with extracting the minimum and maximum value of an array. Take a look at the following code snippet to see how it works.

NSArray *numbers = @[ @(1), @(2), @(45), @(-12), @(3.14), @(384) ];

// Minimum
id min = numbers.min(^(NSNumber *n) {
    return n.intValue;
});

id max = numbers.max(^(NSNumber *n) {
    return n.intValue;
});

NSLog(@"\nMIN %@\nMAX %@", min, max);

The above code snippet results in the following output.

MIN -12
MAX 384

The syntax may seem odd and you may be wondering why min and max take a block, but this actually adds more power to theses methods. You can do whatever you like in the block to determine what the minimum and maximum value of the array is. The following example should clarify this.

NSArray *words = @[ @"this", @"is", @"a", @"example", @"for", @"everyone" ];

// Minimum
id shortest = words.min(^(NSString *n) {
    return (NSInteger)n.length;
});

id longest = words.max(^(NSString *n) {
    return (NSInteger)n.length;
});

NSLog(@"\nSHORTEST %@\nLONGEST  %@", shortest, longest);

This code snippet results in the following output.

SHORTEST a
LONGEST  everyone

YOLOKit is flexible and doesn't complain about the type of the block arguments. However, to satisfy the compiler, we cast the return value of the block to NSInteger, because that's what it expects.

Filtering Arrays

Selecting & Rejecting

There are a number of methods to filter arrays, including select and reject. Let's see how we can filter the array of numbers and words we created earlier.

NSArray *filteredNumbers = numbers.select(^(NSNumber *n) {
    return n.intValue > 10;
});

NSLog(@"FILTERED NUMBERS\n%@", filteredNumbers);

NSArray *filteredWords = words.reject(^(NSString *n) {
    return n.length <= 2;
});

NSLog(@"FILTERED WORDS\n%@", filteredWords);

You have to admit that this is very nice to look at. It's concise and very legible. The arrays in the above examples are simple, but note that you can use arrays that are much more complex than this. The following example illustrates this.

NSArray *people = @[ person1, person2, person3, person4, person5, person6 ];

NSArray *males = people.select(^(Person *p) {
    return p.sex == 0;
});

NSArray *females = people.reject(^(Person *p) {
    return p.sex == 0;
});

Subarrays

YOLOKit also defines first and last, but they don't do what you expect them to do. In other words, they're not equivalent to NSArray's firstObject and lastObject methods. With first and last you can create a subarray from the original array. Take a look at the following example.

NSArray *subsetNumbers = numbers.first(3);
NSArray *subsetWords = words.last(2);
NSLog(@"SUBSET NUMBERS\n%@", subsetNumbers);
NSLog(@"SUBSET WORDS\n%@", subsetWords);

The above code snippet results in the following output.

SUBSET NUMBERS
(
    1,
    2,
    45
)
SUBSET WORDS
(
    for,
    everyone
)

Manipulating Arrays

Sorting

Sorting an array is trivial with YOLOKit. Let's see what it takes to sort the array of numbers we created earlier. It's that easy.

NSArray *sortedNumbers = numbers.sort;
NSLog(@"%@", sortedNumbers);

Uniquing

One of the benefits of using NSSet is that it doesn't contain duplicate objects. However, uniquing an array of objects is trivial with YOLOKit. Let's add a few additional numbers with YOLOKit's concat method and then unique the array with uniq.

// Concatenate
numbers = numbers.concat(@[@1, @2, @3, @4]);

NSLog(@"CONCAT %@", numbers);

// Unique & Sort
numbers = numbers.uniq.sort;

NSLog(@"UNIQ %@", numbers);

Have you noticed I also sorted the array by chaining uniq and sort? The goal isn't to turn Objective-C code into Ruby or JavaScript, but I'm sure you agree that this code snippet is concise, and very easy to read and understand.

Reversing & Shuffling

// Reversing
NSArray *reversedNumbers = numbers.reverse;
// Shuffling
NSArray *shuffledWords = words.shuffle;
NSLog(@"REVERSED\n%@", reversedNumbers);
NSLog(@"SHUFFLED\n%@", shuffledWords);

The above code snippet results in the following output.

REVERSED
(
    384,
    "3.14",
    "-12",
    45,
    2,
    1
)
SHUFFLED
(
    for,
    is,
    everyone,
    example,
    a,
    this
)

Other Methods

There are a lot of other methods to work with arrays, such as rotate, sample, without, set, transpose, etc. I encourage you to browse YOLOKit on GitHub to find out more about them.

There are also methods that can be used with NSDictionary, NSNumber, and NSString. The following code snippet shows you how to convert a string into an array of words.

id wordsInString = @"You only live once. Right?".split(@" ");

NSLog(@"STRING %@", wordsInString);
STRING (
    You,
    only,
    live,
    "once.",
    "Right?"
)

3. Considerations

Code Completion

Because of YOLOKit's odd syntax, Xcode won't be of much help when it comes to code completion. It will show you a list of suggestions for YOLOKit's methods, but that's about it. If you want to use YOLOKit, you'll have learn the syntax.

Performance

YOLOKit isn't optimized for performance as this GitHub issue shows. However, it does make your code prettier and more readable. Using a for loop to loop over an array will be faster and more performant than YOLOKit's methods and it's important that you keep this in mind.

Conclusion

Do I recommend YOLOKit? Yes and no. The above considerations shouldn't keep you from using YOLOKit, but make sure that you don't use YOLOKit if performance is important, because there are better options available—like the good ol' for loop.

The long of the short is that you should only use YOLOKit if you feel that it adds value to your project. Also consider that your colleagues need to learn and appreciate YOLOKit's syntax. I think YOLOKit is a great project that clearly shows how incredibly expressive Objective-C can be. For me, that's the most important lesson I take away from YOLOKit.

2014-05-30T13:58:10.000Z2014-05-30T13:58:10.000ZBart Jacobshttp://code.tutsplus.com/tutorials/having-fun-with-yolokit--cms-21277

Viewing all articles
Browse latest Browse all 1836

Trending Articles