Wednesday
05Dec2007
What are XIB files?
Wednesday, December 5, 2007 at 09:03AM
The Xcode tools in Leopard introduced a new file format for user interfaces. In the brach of the prehistory of Mac OS X that includes NeXT, interfaces were defined in files known as NIBs, from their .nib extension.
A NIB file is a Mac OS X package containing, usually, a keyedobjects.nib file and a classes.nib file. When building an application, these files are copied into the app's bundle and are loaded at runtime to provide the user interface for the application. For what comes next, bear in mind that the NIB in your Xcode project is the same NIB that is loaded at runtime in the application.
Interface Builder 3 on Leopard introduces a new file format: XIB. The developer experience of editing XIB files in Interface Builder is identical to editing a NIB file but, in many other ways a XIB is radically different from a NIB file. If I had one criticism of WWDC '07, it was that they didn't spend enough time explaining XIBs and their implications.
File Format
Firstly, the XIB file format is completely different from that of a NIB. A XIB is not a package - it is a single self-contained XML file. The XML schema is specific to XIBs - it is not a simple XML Property List.
This brings a couple of advantages. Firstly, it's a benefit for users of tools that are not package-aware. In early days, Interface Builder would blow away the
Secondly, the file format is vaguely human-understandable. It is very dense and not entirely obvious, but one can make some sense of it. This helps in some of those last-ditch situations where you're grepping the file to find out where a particular binding is referenced.
It is worth pointing out two things that this text-based file format does not immediately deliver: hand-editability and mergability. The XIB file is an XML representation of an object graph and, as such, it is considered dangerous to edit it by hand. Merging is just a machine-assisted form of hand editing and is likely problematic. However, the text format might assist in understanding where the conflicts lie.
Working with XIBs
The primary tool for editing XIB files is, of course, Interface Builder. For many years, there has been a command line tool for manipulating nibs called
The man page for ibtool contains a lot of detail, but it's worth noting a couple of new capabilities: the
There are also many options in
Deploying XIBs
The crucial difference between XIB and NIB for deployment is that a XIB is not a deployable file. Xcode 3 will compile a XIB into a deployable NIB when the project is built and will include that deployable NIB in the finished application.
If you're using XIB in your Xcode projects, you might see a CompileXIB build operation going past. What's actually happening there is that Xcode is calling:
It's
The NIB file produced by compiling a XIB file in
XIB Wishlist
Interface Builder 3, XIBs, and the new capabilities of
The one remaining thing I really want to see in this tool set is the ability to diff and ultimately merge XIB files, but I would be very happy to just get diff capabilities right now. It doesn't even need to be graphical - just a text report would be great. It seems that the XIB file format lays the foundation for something like that to happen in the future. Let's hope we get there.
Further Reading
There's not a huge amount about XIBs in there, but the Interface Builder 3.0 Release Notes are worth reading.
[Update: Jens Ayton pointed out that I had, incorrectly, used the term 'bundle' in a number of places to refer to NIB files which are, in fact, packages.]
A NIB file is a Mac OS X package containing, usually, a keyedobjects.nib file and a classes.nib file. When building an application, these files are copied into the app's bundle and are loaded at runtime to provide the user interface for the application. For what comes next, bear in mind that the NIB in your Xcode project is the same NIB that is loaded at runtime in the application.
Interface Builder 3 on Leopard introduces a new file format: XIB. The developer experience of editing XIB files in Interface Builder is identical to editing a NIB file but, in many other ways a XIB is radically different from a NIB file. If I had one criticism of WWDC '07, it was that they didn't spend enough time explaining XIBs and their implications.
File Format
Firstly, the XIB file format is completely different from that of a NIB. A XIB is not a package - it is a single self-contained XML file. The XML schema is specific to XIBs - it is not a simple XML Property List.
This brings a couple of advantages. Firstly, it's a benefit for users of tools that are not package-aware. In early days, Interface Builder would blow away the
.svn directory inside the NIB package. That hasn't been a problem for years, but other tools may still have issues.Secondly, the file format is vaguely human-understandable. It is very dense and not entirely obvious, but one can make some sense of it. This helps in some of those last-ditch situations where you're grepping the file to find out where a particular binding is referenced.
It is worth pointing out two things that this text-based file format does not immediately deliver: hand-editability and mergability. The XIB file is an XML representation of an object graph and, as such, it is considered dangerous to edit it by hand. Merging is just a machine-assisted form of hand editing and is likely problematic. However, the text format might assist in understanding where the conflicts lie.
Working with XIBs
The primary tool for editing XIB files is, of course, Interface Builder. For many years, there has been a command line tool for manipulating nibs called
nibtool. In Leopard, nibtool has been renamed (or replaced - I'm not sure) by ibtool and has acquired many new tricks.The man page for ibtool contains a lot of detail, but it's worth noting a couple of new capabilities: the
--convert option will rename old classes to new. Presumably it is ibtool --convert that allows Xcode 3.0's refactoring tools to reach into interface files.There are also many options in
ibtool for printing the details of a NIB or XIB to an XML Property List, including --objects, --connections, --hierarchy and --classes, although a mode to conveniently list the bindings in an interface sadly isn't available.Deploying XIBs
The crucial difference between XIB and NIB for deployment is that a XIB is not a deployable file. Xcode 3 will compile a XIB into a deployable NIB when the project is built and will include that deployable NIB in the finished application.
If you're using XIB in your Xcode projects, you might see a CompileXIB build operation going past. What's actually happening there is that Xcode is calling:
ibtool --errors --warnings --notices --output-format human-readable-text --compile your.xib output.nibIt's
ibtool that compiles XIB to NIB, not Xcode itself, and it emits useful warnings and errors about your XIB files right in the Xcode build window. I've found this to be very, very valuable when working with my XIB-based projects.The NIB file produced by compiling a XIB file in
ibtool is a deployment-only file. Unlike most earlier NIB files, one cannot extract a deployment NIB file from an application and open it in Interface Builder for inspection. This is slightly sad, since it makes it harder to learn how others did things. However, in recent years, it has rarely been a worthwhile learning experience to open deployed NIBs, since all one usually finds is a morass of "Custom View" panels.XIB Wishlist
Interface Builder 3, XIBs, and the new capabilities of
ibtool are a great leap forward in Mac development. I've been using XIB files exclusively in my Leopard-only development. I haven't yet converted FlickrExport to be XIB-based. They work perfectly for me, but I have heard of the occasional problem with opening a XIB-compiled NIB on Tiger, so I'm not sure that I'll adopt XIB files in code that has to be Tiger-compatible.The one remaining thing I really want to see in this tool set is the ability to diff and ultimately merge XIB files, but I would be very happy to just get diff capabilities right now. It doesn't even need to be graphical - just a text report would be great. It seems that the XIB file format lays the foundation for something like that to happen in the future. Let's hope we get there.
Further Reading
There's not a huge amount about XIBs in there, but the Interface Builder 3.0 Release Notes are worth reading.
[Update: Jens Ayton pointed out that I had, incorrectly, used the term 'bundle' in a number of places to refer to NIB files which are, in fact, packages.]
in
Programming,
Tech
Programming,
Tech
Reader Comments (8)
Great article. While doing a straight text diff of XIB files is currently daunting, you may find it useful to pipe XIB's through "ibtool --objects --connections file.xib" and then diff the results.
Bloody good post, IMHO.
That's about all I have to say on the subject :)
Thanks for this post. A quick note is that it looks like the final 2 arguments for your ibtool compile command should actually be reversed, so it becomes:
ibtool --errors --warnings --notices --output-format human-readable-text --compile output.nib MainMenu.xib
Hi Frasier,
Forgive me if this is a dumb question, but I'm new to Xcode and Cocoa.
How do I use xib files? The documentation simply says to save the nib as a xib. It doesn't say anything else, so apart from the fact that the xib file is on disk, I can't find any other reference to it in my Xcode project, nor find any way of adding it to my project.
Do I have to add it? Do I have to remove the existing nib file? Does 'Saving As..." make it active by default?
I've searched far and wide through both the documentation and the internet and cannot find any answer to the question of how to use a xib file. That probably means it's a dumb question, so apologies in advance. Help gratefully appreciated.
James,
Yes, the default Xcode templates all use NIBs rather than XIBs. To convert a NIB to a XIB, you would:
- Open the NIB
- Choose File / Save As...
- From the File Type menu choose the one that ends in "(XIB 3.x)"
At this point, you'll have a .nib file and a .xib file. You can just delete the .nib file and drag the XIB file into the XCode project at the same place that the NIB was. You'll need to delete the reference to the NIB file from your project too (select it and press backspace).
Fraser,
Thanks so much for your quick reply.
Incidentally, I keep running into great stuff by you - interviews etc. Thanks so much for those. Not only are you one of the main sources of good information, but what you say is always clean and clear.
Best regards and thanks!
James
Did you mean "branch", Fraser? You've got "brach" ... or is it some Scots term I don't know? :-)
"A NIB file is a Mac OS X bundle containing, usually, a keyedobjects.nib file and a classes.nib file. When building an application, these files are copied into the app’s bundle and are loaded at runtime to provide the user interface for the application."
Don't nibs have a third file, info.nib as well? Also, AFAIK, not all these are loaded at runtime. Apple says:
"These are wrappers, ending in .nib that contain three files: classes.nib, info.nib, and keyedobjects.nib. These files can be opened for development with any version of Interface Builder. Only the keyedobjects.nib file is required at runtime, and these files are deployable on any recent Mac OS X version."
http://developer.apple.com/releasenotes/DeveloperTools/RN-InterfaceBuilder/
so does anyone know how large teams merge conflicting NIB/XIB files when the versioning system cannot merge them? for example, does a human have to understand all those unique ids?