Mar 4, 2016

NACommonUtils - Swift utilities on GitHub

I’ve released some common Swift utilities and extensions that I use across a number of iOS apps on GitHub

Utility Comment
ActivityOverlay Light-weight version of MBProgessHUD - shows a translucent HUD, containing an activity indicator, centred in a specified view
Array+Functional func headTail() -> (head: Element, tail: [Element])? see list monster
NSMutableAttributedString+Creation NSMutableAttributedString creation helpers
OnePixelConstraint Designed as a auto-layout width/height constraint that will always be 1px regardless of screen scale
String+LineUtils Strings line helpers
UIButton+ActionBlock Button onPressed: block extension
UIResponder+FindUIViewController Walk the responder chain until we find a UIViewController; useful when a UIView needs to access UIViewController API
UIView+Autolayout Autolayout helpers; useAutolayout(), centerInView(..), constrainToWidth(..), constrainToHeight(..)
KeepInMemoryMixin Class mixin to allow the class to keep itself in memory
UIView+NibLoading instanceFromNib() Load an instance of a view from a nib named identically to the class.
UIStoryboard+InstantiateViewController type-safe Storyboard view controller instantiation
UIView+border layer based UIView border utilities
More »



Feb 19, 2016

iDiff View available on the app store

iDiff View highlights the differences between two versions of a text file.

The left pane shows the older file and the right pane shows the more recent version:

More »



Feb 10, 2016

Profit from Futures

As most battle-hardened programmers will attest, implementing threaded code that reads or writes to shared mutable state is hard to develop correctly and rapidly degenerates into a maintenance nightmare unless all programmers are equally skilled. Lets review why; shared mutable state requires locks and:

  • Locks do not compose
  • Locks break encapsulation (you need to know a lot!)
  • Error recovery is hard
  • Its easy to get locks wrong by:
    • Taking too few locks
    • Taking too many locks
    • Taking the wrong locks
    • Taking locks in wrong order

In summary unless handled with extreme caution, threaded code tends to lead to impossible to reproduce bugs - it’s non deterministic - you only find out there’s a problem when you see the crash reports.

More »



Feb 2, 2016

Superpowers / obfuscation with map & flatMap

Many Swift developers will reach for map when they want to transform the elements of an array:

let twos = (1...10).map { $0 * 2 }
twos // [2,4,6 ... 20]

however fewer developers use map with an Optional:

// UIImage(named:) has a signature: String -> UIImage?
let imageSize = UIImage(named: "anImage").map {$0.size}
imageSize // a 'CGSize?' or more explicitly 'Optional<CGSize>'

This is only of limited interest for Optional as Swift has simpler built-in syntax for handling such cases.

More »



Jan 28, 2016

Zip3

The Swift standard library includes zip which creates a single array containing two element tuples from two arrays. However I wanted to zip three arrays of previous, next and current values. With open-source Swift it was straight-forward to examine the standard library implementation of zip and create Zip3 :-

More »