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 above code snippet results in the following output.

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.

This code snippet results in the following output.

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.

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.

The above code snippet results in the following output.

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.

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.

Reversing & Shuffling

The above code snippet results in the following output.

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.

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.

Tags:

Comments

Related Articles