ViaSwiftUtils - Extending the Swift Standard Library at Viacom

How we got here

Let's begin with how Viacom developers and Swift came together, but if you want the Tl;DR click here.

Like many other  developers, when Swift was announced I was over the moon.

Not just for me personally, but also because I realized that most of the bugs we solved in our ObjC code at Viacom were due to:

  • nil where we didn't expect it
  • type-mismatches
  • obfuscated code and obscure API's

With a statically typed language and explicit optionals, most of these problems would be weeded out by the compiler.
Furthermore, Swift:

  • is easier to read (except for Smalltalk-veterans), so it's easier to onboard engineers
  • has no more .h/.m, less boilerplate, less code-duplication, and generics
  • while 'less code' leads to fast development and fast scanning of existing logic with human eyes, it also means less legacy code to maintain
  • is more fun. Coding with playgrounds enables interactive development
  • is faster and uses less memory (especially when utilizing value types)
  • more 1

So after WWDC 2015, I presented this enthusiastically to my product managers, directors and most importantly my fellow developers. Despite some concerns — Swift 2 was just release and the constant Swift evolution was scary — we built our next big project entirely in Swift.
The project called Playplex is now the platform for many Viacom brand apps including TVLand, MTV, ComedyCentral, BET, CMT and more. 2

ViaSwiftUtils

Now while working on this project we created many small helper extensions to types provided by the Swift standard library.

Every Swift project we saw tended to have these and usually they addressed the same common problems. So, we created one common bucket for all those small utility functions that we use all the time, called ViaSwiftUtils.

This enabled us to share bug fixes, share a thorough test suite, and when Swift evolved, we only had to adjust our helper methods once.

Today we are proud to share these with the world!

So what can ViaSwiftUtils do for you? Here are some examples we compiled:


Tired of spelling out NSLocalizedString every time you localize a string?

title = "HomePage.title".localized  
print(title) // 'Main', '頭版', 'صفحه نخست', 'Startseite'  

Want to find the longest word in a string?

print(germanText.longestWord())  
// 'Verkehrsinfrastrukturfinanzierungsgesellschaft'

Need an array (or any other MutableCollection) shuffled? 3

var numbers = [1, 2, 3, 4, 5, 6]  
numbers.shuffleInPlace() //e.g. [4, 1, 5, 2, 6, 3]  

Want all the unique entries in a Sequence with Hashable elements?

let emojis = ["😀", "👀", "😱", "😡", "👀", "😀", "👀", "😱"]  
let uniqueEmojis = emojis.unique() // ["😀", "👀", "😱", "😡"]  

Is dealing with TimeInterval inconvenient?

let twelveDays = 12 * TimeInterval.day  
let minutes = twelveDays / TimeInterval.minute // 17280  

Need to create an image with rounded corners from an existing image?


Does one of the views in your app need to rotate?

someView.startRotating()  
// ...
someView.stopRotating()  

And more date helper methods & variables:

let components = DateComponents(calendar: gregorian, era: 0,  
                                year: 44, month: 3, day: 15, hour: 13)
let idesOfMarch = gregorian.date(from: components)!  
let weekDay = idesOfMarch.dayOfWeek // 6  
let daysSince = idesOfMarch.days(to: Date()) // 752533  

 

These are just a few examples. We are expanding the library continuously and we accept Pull-Requests ☺️

 

How to install ViaSwiftUtils

 

Via Cocoapods

Add the following line to your Podfile.
Remember you'll need to enable use_frameworks!.

pod 'ViaSwiftUtils', '1.0.1'  
Via Carthage

Add the following line to your Cartfile

github "ViacomInc/ViaSwiftUtils" ~> 1.0.1  
Via Swift package manager

Add the following to your Package.swift dependencies

.Package(url: "https://github.com/ViacomInc/ViaSwiftUtils", majorVersion: 1, minor: 0),

I should mention that only the Foundation functions that are part of ViaSwiftUtils work via SPM, as UIKit is not available on Linux or MacOS.

Importing

After you installed the framework via one of the 3 above methods, import it in your .swift file.

import ViaSwiftUtils  

 

Tl;dr:

Some iOS Devs at Viacom put together a collection of miscellaneous helper methods into an open source library called ViaSwiftUtils .

 

Acknowledgements

All Viacom INTL iOS developers contributed to the ViaSwiftUtils library.We also received a lot of support and encouragement to finally release it from Viacom management.

We want to thank  and all developers around the world who contribute to Swift for providing us with our favorite productivity tool.

Special thanks go to Chris & Florian from objc.io for the Swift training they provided.

# Footnotes
  1. Plenty of articles have been written on the benefits of Swift, I recommend this or this one.

  2. In 2015 we started with Latin America but apps are rolled out across the globe, so some might not yet be available in your countries app store.

  3. This method is mostly based on examples from this book. If you already know how to program in Java or ObjC and want to start with Swift, it's the one you should read. Not kidding!

Konrad Feiler

iOS architect at Viacom INTL, mathematician at heart but also really into history, ants and ukuleles