Category Archives: Uncategorized

Loading Swift UIViewController from XIB in storyboard

Some developers prefer to keep ViewControllers design in separate XIB files instead of placing everything in Storyboard. Especially on displays with the smaller real estate (like working on a MacBook), navigating and editing the storyboard could be tedious.

There is a simple technique which lets us keep storyboard ViewControllers and segues between. Following one of the tutorials available is simple:

  1. Place UIViewController on storyboard
  2. Highlight its View and delete it. You should now have “hollow” ViewController.
  3. In identity inspector, set the custom class to name of your choice.
  4. Click File -> New, create new Cocoa Touch subclass of UIViewController. Don’t forget to check “Also create XIB file“. Name the class exactly as on Storyboard.

It works perfectly with Objective-C projects – iOS takes care of loading view from XIB, provided it has the exactly same name as the class. But if the class is written in Swift, all you get is black screen.

Fixing on Swift side

Apparently this smart mechanism is broken newest Xcode builds (including GM). Fortunately, simple remedies exist.

Name the XIB

Just change name of XIB file to ModuleName.ClassName.xib and view should be loaded.

Load XIB programmatically

If renaming XIBs is not appealing to you, you may keep XIB name as it is, and override loadView: method with short snippet of code:

November 2016 Update

In Xcode 8.x/Swift 3.x naming the XIB in smart way seems to not to work anymore. Still there is programmatic way, which may be preferred as more explicit. In Swift 3, loading code would look slightly different, because of different conventions of mapping Foundation classes to Swift:

loadView is responsible for creating ViewController’s view. Override it to get control over view creation process. 

Xcode 6 beta 5 update

With release of Xcode 6 beta 5, cat and mouse game continues: virtually every open source project/library written in Swift, needs to be updated. Not complaining at all, let’s see what I needed to update in example Actor library project and what is probably waiting for you to encounter.

LogicValue is now BooleanType

Swift engineers are willing to make naming consistent, and all built-in protocols shall be called something-able, -ible or -Type. If you happen to have implemented LogicValue somewhere:

  1. Rename it to BooleanType
  2. Remove getLogicValue() -> Bool function, moving its body to:

New BooleanType protocol defines not a function, as it was in LogicValue, but required property. Now we have a bit of extra consistency.

Optionals are no longer LogicValue (nor BooleanType)

To prevent ambiguous constructs, you can no longer use Optional value in if statement, now it needs to be explicitly compared with nil. I have used it in my functional extension to Optional, which I wrote about, and which has been rendered almost useless with Beta 5.

Elvis operator

Short ?? (two question marks) operator does the same as Optional+getOrElse() extension.

Converting String to C String

Apparently String.bridgeToObjectiveC() is gone, and I needed to find other way of converting Swift’s Strings to C Strings. Existing code which creates serial dispatch queue:

Now is replaced by cleaner code:

Required keyword in initializers

Any subclass inheriting required initializer need to place ‘required’ keyword in front of each required initializer.

This concludes my opinionated, subjective list of breaking changes of Beta 5. I hope the more obscure ones will be found helpful!

 

Reboot

This is unintentional reboot of blog and website. It used to be hosted on friends server provided to me as a courtesy. It ran there for years, and suddenly stopped functioning.

The friends server has been sunset, and he forgot to notify me. Looks like I was extremely trouble-free tenant.

It is a good opportunity to start from scratch, possibly revive some posts from archives.