Having Fun with YOLOKit

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.

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.

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.

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.

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.

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.

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.