Functions as Factories

Cocoa at Tumblr:

Factories are a fairly well understood design pattern in software development. The benefits of using factories include:

  1. Abstracting constructors away from clients.
  2. Encapsulating data that clients do not need to know about.
  3. Allowing for more testable code by enforcing the idea of passing objects into initializers instead of referencing singletons directly.

This post will show the power of a few Swift features as well as of firstĀ­-class functions.

Dynamically Sized Table View Header or Footer Using Auto Layout

I’m trying to support Dynamic Type everywhere I can in my new app. Auto Layout and self sizing table view cells (buggy as they are) makes that a lot easier for the most part. Still, there’s other places that just setting up constraints and changing the font size of a label isn’t enough. The one I’ve just dealt with is a table view header (UITableView().tableHeaderView) with text in it. If I do nothing but set up my constraints and set the header view, the font size changing will just cause extra space to appear or my text to get cut off.

The solution was to override UIViewController().viewDidLayoutSubviews(), get the proper size of the header view based on it’s constraints, set the frame on the header, and reset it as the table header view.

It took me a little while to figure this out, so here’s what I did:

You Can Do This In Swift

This isn’t a hidden feature or anything — it’s just how optional binding works — but I guess what I didn’t get before was that this (responseObject’s type is AnyObject):

Could become:

So much better. So much code to delete now. Put this under the category of “things I can’t believe I didn’t get until now.”

UITableView Extension to Deselect Selected Row

Nothing fancy. Just a tiny little extension method that makes doing this in Swift slightly nicer.

Optional Punctuation in Swift

All of the punctuation in Swift looked pretty weird to me until recently, but now that I get it, I’ve really learned to appreciate how great the syntax actually is. The way I think of it is this: if you’re using ?, you’re asking a question, and when you use ! you’re asserting that you already have the answer. Of course, for the second case, you need do need to be sure or your app will blow up.

So, for example:

Asking a Question

if let foo = dictionary["foo"] as? String { ... }

Is the value for the key “foo” a string? If so, assign it to the constant foo.

delegate?.objectDidSomething(self)

Do I have a delegate, and can it respond to objectDidSomething:?

Making an Assertion

managedObjectContext!.save(&error)

I know that managedObjectContext is not nil.

let foo = bar as! String

I know bar is a string, so nothing will go wrong casting it.

My Everything Through the Xbox One Setup

I haven’t posted in a while due to a bunch of reasons, and I’d like to get into a rhythm again. I thought I’d start by sharing the kind of unique way I have my media stuff running through and Xbox One.

I’ve played a bunch of games on the Xbox since I got it in November — and a bunch of them have been great. But the Kinect and HDMI in are the unique part I wanted to mention. My set up is that I have a Kinivio HDMI switcher with a TiVo, Apple TV, and Wii U going into it, and the switchers output plugged into the Xbox. The Xbox can control be setup to control TV functions, and with the Kinect it can do those with voice commands (“volume up/down”, “mute”, “pause”, etc). Since I have everything running through my Xbox, I can keep using those commands with any of my other devices.

Having a bunch of things running through this specific HDMI switcher works especially well because it automatically switcher to any input that start getting a new signal, so I rarely have to manually switch inputs.

The only downside is that I have to have my Xbox One on to use anything else, but in practice that hasn’t been annoying at all.

Brent Is Going to Omni

Brent Simmons is not only a friend, but one of my favorite Cocoa developers. His blog is the most indispensable about writing software I read. Omni is one of my favorite Cocoa development companies. Can’t wait to see what they do together.

The Jefferies Project

The Jefferies Project

What if… in a few years from now, we figure out a way to make deep space travel possible. What if… the first interstellar spacecraft to be built is commissioned by the US Navy and its design is inspired by a science-fiction TV show from the sixties… what would it look like?

Neat! I don’t know why there’s so much emphasis on guns though. Who is it supposed to be shooting at?