Last week as I was merging code to my working branch, I noticed something I had rarely (never?) seen before:
Git had just auto-merged a xib file for me! I figured this must have something to do with Xcode 5 and its “upgrading” of xib files, so I started digging.
It turns out that the xib files generated by Xcode 5 are of a completely new kind. It seems that Apple started from scratch with this and the changes make xib files finally usable in teams of all sizes for the first time.
Xcode 4 xib format
To demonstrate the difference between the 2 file formats I have created a new xib file in Xcode 4 and put a bunch of views in it: A UIScrollView with UIButtons, UILabels and more in it, a UITextView etc.
After that I right clicked on the file in the project navigator and selected
Open As > Source Code. Here is what that file looked like under the hood:
.xib file generated by Xcode 4 (you need to click “File suppressed. Click to show.” because github is being stupid)
1108 lines for a rather simple view! That’s a lot.
Try to make sense of the file, it’s not intelligible at least not easily. This is the main reason why people don’t like xib files and don’t use interface builder.
It gets worse: the format is non-deterministic. Meaning that if you and I create exactly the same UI in Interface builder, we might (will) not have the same file content. This use to make xib files very difficult or even impossible to merge. This is the other big reason for people not to use xib files.
Xcode 5 xib format
Then I opened the same project in Xcode 5. When you open a xib file that was created in Xcode 4 in Xcode 5 you are prompted to upgrade the file to the new format. You want to only do it if all the people working on the project use Xcode 5 because the new files can’t be opened by older versions of Xcode.
I clicked Upgrade and then opened the xib file as source code again to see what had changed. Here is what it looked like.
.xib file generated by Xcode 5 (again, click “File suppressed. Click to show.”)
103 lines! The old file had more than 10 times as many lines! Now this is what I call a drastic reduction.
And look at it, it’s actually understandable. The file now reflects the view hierarchy that you see in the left part of Interface Builder! With a little bit of practice you could even write that xml code. Ok, ok I am pushing it a little here.
On top of that, the code generated seems to be deterministic now. This means that with the same user interface in input it will generate the same xib file. This makes manual merging much easier if it is needed.
The best part is that this new file format is not only understandable by humans but git can also make sense of the changes happening and will now most of the time auto-merge your xib files for you!
Now if someone tells you that they don’t want to use xib files, send them to this article, I don’t think there are many reasons left not to use them.
This is not the only improvement that Xcode 5 brings to Interface Builder, for example, setting auto-layout constraints is now awesome whereas it was a pain before. If you have tried setting constraints in code you know that it is not a walk in the park.
Let me know what you think on twitter!