Tuesday, February 12th, 2008
A Discussion of Hanselminutes Show #99
In this life, I’m in a peculiar position. During the day I’m a C# / .NET desktop application developer. However for the past several years my evenings have been spent exploring Cocoa and Mac OS X application development. I’m not sure if any of my readership (hi mom) subscribe to Hanselminutes, but the most recent episode, “What’s it like for Mac Developers - Interview with Steven Frank of Panic,” peers into the world of a mac developer from a .NET perspective.
Since I’m in the aforementioned position, I’ve decided to take this opportunity to expand on some of the items in the episode. Some were covered well, and some were glossed over. I’ve tried to garnish the most relevant points of conversation. Consider this post the parsley on Scott and Steven’s well-prepared entree.
Kernel Panics
The OS X kernel panic screen that Steve talks about is shown below:
NOTE: As an aside, the kernel panic screens can be customized, but there’s no way I’m going to vouch for the safety of this technique. Proceed at your own risk.
Applications and Packages
The “Show Package Contents” menu, accessible via ctrl+clicking an application in the Finder:

After clicking the “Show Package Contents” menu item, the finder will reveal the hidden folder structure inside the application. An application (a file with a .app extension) is a type of package in OS X. The Finder treats packages differently than other folders. There are several types of packages, and you can read about them and how the Finder handles them here.
Fat Binaries
The topic of fat binaries, or in Apple’s terms Universal binaries was discussed particularly well. As Steven explained, fat binaries are executables that have been compiled for separate architectures (i386, PowerPC, IA-64) and “glued” together. It’s a single file that contains binaries for multiple architectures, and was one of the techniques Apple employed to make the PowerPC-Intel switch easier for users. I’m not going to describe much here except for a little info on how to poke fat binaries.
file is a command line program to display information about a file. Below is sample file output for a text file:
rhmacbook:~ $ file hello.txt
hello.txt: ASCII text
Here’s the file command run on ls, the command to list the contents of a directory:
rhmacbook:~ $ file /bin/ls
/bin/ls: Mach-O universal binary with 2 architectures
/bin/ls (for architecture i386): Mach-O executable i386
/bin/ls (for architecture ppc7400): Mach-O executable ppc
The output states that the ls command is a Mach-O universal binary including binaries for both the i386 and the ppc7400 architectures. file is a great command in general, and can be used to make a great first guess at a specific file’s purpose.
file provides some rudimentary information about a fat binary, but the lipo command holds the real functionality when dealing with fat binaries. In addition to providing additional information about fat binaries, lipo can also remove, add, and modify the architectures present. As a lazy blog author and technology pundit, I’ll leave the full functionality as an exercise for the reader (hint: read the man page for lipo). However, to give you just a taste I’ll run lipo -detailed_info on ls:
rhmacbook:~ $ lipo -detailed_info /bin/ls
Fat header in: /bin/ls
fat_magic 0xcafebabe
nfat_arch 2
architecture i386
cputype CPU_TYPE_I386
cpusubtype CPU_SUBTYPE_I386_ALL
offset 4096
size 36464
align 2^12 (4096)
architecture ppc7400
cputype CPU_TYPE_POWERPC
cpusubtype CPU_SUBTYPE_POWERPC_7400
offset 40960
size 32736
align 2^12 (4096)
I’m not even going to pretend to know what some of this information is, but it appears to provide more detailed_info (snicker) about the various structures of the binaries included in the file. One thing I’d like to note is the magic number for a fat binary is 0xcafebabe. The only, very geeky, reason this is of interest is that the Java .class shares 0xcafebabe as a magic number. I brought this up in #macdev on freenode and was quickly informed that “fat binaries used that magic number long before Java was around.” And that, my friends, is our neat historical tidbit of the day.
Interfaces (like IPerson) in Cocoa
The discussion on interfaces was on area that needed a little more roughing out. Steven was a little vague about whether interface-like functionality could be achieved in Objective-C. Interfaces are implemented in Objective-C via formal protocols. There only caveat here is that a class can implement a formal protocol without actually declaring that it does. That’s a little weird, I know, but just roll with it.
Frameworks
Frameworks are the library objects in Cocoa, similar to assemblies in .NET. As Steven mentioned in the podcast, Objective-C is just an object-oriented superset of C, so Objective-C uses C-like libraries at it’s core. While .NET uses embedded metadata to provide information about the assembly contents, Objective-C still requires .h header files to describe library contents.
Frameworks provide a tidy way of bundling both the header files and libraries together in one logical package. The Objective-C compiler (gcc) can then use these frameworks as arguments to the linker flag. With XCode a lot of this is handled automatically. However, a developer may choose to compile from the command line. Frameworks ease the pain of traditional C builds, since referencing a framework will provide both the header files and library files, as opposed to listing include and linker directories as arguments to the compiler.
Frameworks also provide some additional support infrastructure for versioning and non-code resources. A little more information can be found at Wikipedia.
Building UIs with Interface Builder
Apple’s tool for creating user interfaces in OS X is Interface Builder. Interface Builder is very similar to the designer in Visual Studio in terms of function. However, the structure of the data generated by the two tools are very different. The Visual Studio designer generates code, and as of VS2005 in the C# world GUIs are described by a designer partial class file. The layout and structure of the GUI is described in code in this file. So, the designer uses code-generation for UI development.
While Interface Builder is another drag-and-drop WYSIWYG editor, the GUI objects are actually saved as binary data (sometimes called freeze drying) to a .nib (NeXT Interface Builder) file. Object interconnections are also stored in this file. At run-time the objects are simply unpacked from the file and attached to the binary.

The Importance of Model-View-Controller in Cocoa Development
Cocoa development adheres strongly to the Model-View-Controller design pattern. Almost all cocoa frameworks provided by Apple fall into this grouping. From the views in Interface Builder to the models generated by Core Data, MVC can be seen throughout the Cocoa architecture.
Bindings
I mentioned MVC in the previous paragraph to introduce the bindings technology that was introduced in OS X 10.3. Bindings are very similar to data binding in .NET (for example, binding a DataGridControl to a DataSource). Binding technology allows developers to link data model objects directly to view objects, in an MVC sense. Thus, bindings allow the developer to skip writing some controller-layer logic entirely.
For example, say I have a GUI that has a series of text boxes used for personal information data entry. The information entered might be something like first name, last name, address, age, favorite child’s name, etc. Let’s also, for the sake of continuity, say I have a person class that has strings that represent all this information. With the proper bindings orchestration I would be able to “bind” (now it gets clearer) the text boxes to the fields of the person class without writing any “glue code” (aka controller code). This would free me from having to catch events on those text boxes and synchronize the person object with the information the user entered.




