Behaviours and Xcode 6

I wrote about Behaviours in newest issue of objc.io Architecture series.

Xcode 6 brought some great improvements to interface builder, one of the coolest one is ability to design custom views, even cooler is the fact that few people realised - IBInspectable works on ANY object, this makes my Behaviours even better!

Behaviours

My behaviours are built on top of 3 principles:

  1. Use of runtime attributes for setting properties
  2. Reversing lifetime notation
  3. Generating events

Both 2. and 3. were easy to use since they were powered by visual binding, it was only a matter of connecting proper outlets/actions.

Using runtime attributes was far from ideal: 1. Error prone - since you need to enter keyPath correctly, spelling mistakes will lead to crash when deserialising Xib/Storyboard which gets annoying. 2. Need to read documentation / header to know what keyPaths are supported.

Enter Xcode 6

With introduction of Xcode 6 we’ve received live rendering of custom views (finally!?), to support custom views rendering you need to: 1. Put your view classes inside a separate framework 2. Specify IBDesignable attribute for your class interface 3. For each configurable custom property you should specify IBInspectable attribute

If we add few IBInspectables like so:

Interface builder will now generate WYSIWYG editors for them:

This is great improvement to workflow, especially if you look at how many different types of properties receive their own editors: - boolean - number - string - rectangle - point - size - color - range - image

This is great improvement to workflow, especially when I work with designers, I no longer need them to read docs for each custom view we have in app, they can just use visual editor 100% of time!

How does that help with Behaviours?

Very few people realised that IBInspectable can work without IBDesignable, and it get’s better:

IBInspectable works without needing frameworks, on any class.

That means that each Behaviour property can be made IBInspectable and you get all the benefits of WYSIWYG editor for no cost, how great is that?

Finally my Parallax behaviour get a nice visual editor:

Supporting older Xcode?

Xcode 6 is still beta and so are inspectables, fortunately for us Apple did great job of joining Runtime attributes and inspectables together:

If you have runtime attribute set for a specific keyPath that’s also IBInspectable property, it will show up as the value of that Inspectable

To be able to compile your IBInspectable enabled Behaviours on older Xcode 5 you just need to add empty macro for IBInspectable, like so:

1
2
3
#ifndef IBInspectable
    #define IBInspectable
#endif

All behaviours will now work as expected, you can use runtime attributes in Xcode 5 or Inspectable’s in Xcode 6.

Grab example code from GitHub.

If you’d like to chat about Behaviours or anything else, send me a tweet.