Yannick Weiss

iOS & Swift

Create images for watchOS

Requires icon.png without alpha channel. I recommend to create and test the 80px and 196px size icons manually.

sips -Z 48 icon.png --out 1-icon-notification-38mm.png
sips -Z 55 icon.png --out 2-icon-notification-42mm.png
sips -Z 58 icon.png --out 3-icon-companion-2x.png
sips -Z 87 icon.png --out 4-icon-companion-3x.png
sips -Z 80 icon.png --out 5-icon-long-look-38mm.png
sips -Z 88 icon.png --out 6-icon-long-look-42mm.png
sips -Z 172 icon.png --out 7-icon-short-look-38mm.png
sips -Z 196 icon.png --out 8-icon-short-look-42mm.png

Adding .gitignore

The first step on every new Xcode project.

curl https://raw.githubusercontent.com/github/gitignore/master/Objective-C.gitignore -o .gitignore

curl https://raw.githubusercontent.com/github/gitignore/master/Swift.gitignore -o .gitignore

Updating to the newest git version

brew install git
sudo mv /usr/bin/git /usr/bin/git-apple

Bypassing Autolayout Constraints

As mentioned in the WWDC session Mysteries of Auto Layout, Part 2 it is possible to set the frame of a view while Autolayout is turned on. For example if you have a complex layout that needs to deal with device orientation.

Override viewDidLayoutSubviews, inside set the view property translatesAutoresizingMaskIntoConstraints to true and set the views frame.

override func viewDidLayoutSubviews() {
    let child = container.viewWithTag(1337)!
    child.translatesAutoresizingMaskIntoConstraints = true
    child.frame.origin.x = 3

Archiving Enums with Raw Values

enum Color: String {
    case Red
    case Blue

When strings are used for raw values, the implicit value for each case is the text of that case’s name. In the example above, Color.Red has an implicit raw value of "Red", and so on.

We cannot save an array of enums into NSUserDefaults, but we can use the rawValue property to create an array of strings.

let defaults = NSUserDefaults.standardUserDefaults()
var colors: [Color] = [.Red, .Blue, .Red] // [Red, Blue, Red]
let colorsString = colors.map({$0.rawValue}) // ["Red", "Blue", "Red"]
defaults.setObject(colorsString, forKey: "colors")

Because we defined a raw value the enumeration automatically receives an initializer that takes a value of the raw value’s type. We can use this to restore our original enum array from the stored data.

if let arr = defaults.arrayForKey("colors") { // ["Red", "Blue", "Red"]
  colors = arr.map({Color(rawValue: $0 as! String)!}) // [Red, Blue, Red]