Simplifying Autolayout Code with Swift

This evening I found myself staring at a block of Autolayout code, thinking to myself: “There must be a better way”. Of course, the ideal is to do all your Autolayout in Interface Builder, but every now and then there are times when you just have to use some code. My code looked something like this:

I understand Apple’s preference for clarity over terseness but, in this case, I think clarity is suffering at the hands of verbosity. With Swift’s default arguments and ability to suppress parameter labeling I think we can do better. My first attempt to simplify the code was to add the following two functions to my project.

With these in place the code becomes:

This is already a great improvement (at least to my eyes), but we can go a little further. There is a lot of repetition of the toolbarView variable in the above code. We can cut this out by extending UIView.

Finally the code becomes:

I think this makes it much clear what’s going on.


Posted in Programming, Swift | 2 Comments

Using CommonCrypto in Swift

Using CommonCrypto in Swift is tricky because it is not a standalone module, so you cannot just import CommonCrypto. In this post I will describe three methods that facilitate using CommonCrypto in Swift and how to choose which method is best for your situation.

Bridging Header

If you simply want to call CommonCrypto routines from your own app, the easiest solution is to add a bridging header to your project and #import <CommonCrypto/CommonCrypto.h> in that header.

If your app is not a mixed source app and you do not already have a bridging header the easiest way to create one is to add a new Objective-C class to your project. Xcode will show an action sheet asking if you want to create one. Once the header is created you can delete the dummy Objective-C class from your project.

You can also add a bridging header manually, but it is a little more tedious. To do this, create a header file and then go to Build Settings and in the section Swift Compiler – Code Generation set the Objective-C Bridging Header to point to the file you just created.

You do not need to import CommonCrypto in your Swift source when using a bridging header.

Local Module Map

Bridging headers work fine for apps, but if you are trying to build a framework to wrap CommonCrypto you cannot use this approach; Xcode does not allow the use of bridging headers in framework targets.

To work around this create a directory called CommonCrypto somewhere. In this directory create a file name module.map and copy the following into the file. You will need to alter the paths to ensure they point to the headers on your system.

To make this module visible to Xcode, go to Build Settings, Swift Compiler – Search Paths and set Import Paths to point to the directory that contains the CommonCrypto directory.

You should now be able to use import CommonCrypto in your Swift code.

You have to set the Import Paths in every project that uses your framework so that Xcode can find it.

Global Fake Module

Swift playgrounds are great for experimenting with code and learning how to use APIs, unfortunately, neither of the above methods make CommonCrypto available in a playground.

The only way I’ve found to work around this is to create a fake CommonCrypto.framework down in the SDK directory where playgrounds look for their files.

To this, open a terminal window and change into the simulator’s Frameworks directory. The easiest way to do this is to use the following command:

Create a directory named CommonCrypto.framework containing the module.map file from the previous section.

You should now be able to create a playground and import CommonCrypto.

If you use this method, CommonCrypto will be available to all apps, framework and playgrounds, although it does “pollute” the SDK as one developer pointed out to me. Notwithstanding this, until an official method emerges, this is the only way!

Calling into CommonCryto

Whichever method you choose to make CommonCrypto available, you will still need to figure out how to call the API from Swift. To say that code to do this is inelegant would be quite an understatement.

The example below shows how to use CommonCrypto to compute an MD5 message digest.

I am working on a framework to make this all much easier, but it is not ready for primetime just yet. You can find it on Github in the IDZSwiftCommonCrypto repository.

Conclusion

The methods presented here have been tested with Xcode 6.0.1 (6A317), changes to Xcode may break them!

If you want to ship a framework using CommonCrypto your clients will beed to use either method 2 or 3 to ensure correct linking.


Posted in Objective-C, Swift, Tutorial | 6 Comments

Swift Standard Library: Zip2

There are lots useful of type, functions, protocols and classes in the Swift Standard Library, many of which have little or no documentation. After finding Swifter a website that extracts what little documentation exists, I have been trying to come up with example code demonstrating them. This is the first post in what I hope will be a series of posts.

Zip2

Given two sequences [ x0, x1 ... xn ] and [ y0, y1 ... yn ], Zip2 produces the sequence [ (x0, y0), (x1, y1) ... (xn, yn) ].

For example, suppose you are writing a Spanish verb conjugator, and you want to prepend the personal pronouns to each form of the verb, you could write:

Or if you are more mathematically minded you could implement a dot product routine:

Download the Playground

The source for this and all other playgrounds in this series can be found on Github in the SwiftStandardLibraryPlaygrounds repository.


Posted in Swift, Tutorial | Leave a comment