YACYAML
I posted YACYAML, the Cocoa YAML parser/object archiver I’ve been working on for a little while to GitHub today. It converts Cocoa objects to and from YAML, a plain text, human friendly data serialization format.
YACYAML can be used in lots of ways - from replacing plists or JSON for simple config files, up to storing entire custom documents in an easy to view (and easy to hand-edit) format.
Why have I made this?
The short answer is that I like YAML. It’s ‘nicer’ than plists and JSON to edit, and it’s far, far, nicer to look at than NSKeyedArchiver’s binary representation (which, to be fair, wasn’t meant to be human-readable in the first place, but I think it would be good if it was).
The emotional appeal
The story behind this is that I was writing yet another configuration file for an iOS app. This one was to configure how CSS generic font families should map to specific on-device fonts in Eucalyptus 2’s UI, given a user’s specific font selection. For example, and simplifying a bit, if the user’s chosen “Baskerville”, the renderer should map “serif” to “Baskerville”, but leave “fixed” things in “Courier”.
This could obviously be specified with a configuration plist:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Baskerville</key>
<dict>
<key>serif</key>
<string>Baskerville</key>
<key>fixed</key>
<string>Courier</string>
…
I really dislike plists though. They’re verbose and hard to edit (and don’t get me started on how they [ab]use XML’s nesting…). The thought of yet another one plist the app saddened me. It would be easier to just hard code this in ObjC.
The obvious solution nowadays is to use JSON:
{ 'Baskerville': { 'serif': 'Baskerville', 'fixed': 'Courier'…
Much, much easier to edit, and much less verbose. If it wasn’t for the fact that I’d used YAML before, perhaps I would have stopped there and used JSON.
I have used YAML before though, so I know things can be better. For all its brevity and ease of editing, JSON still feels like it’s written for a computer. Here’s a YAML representation of the same thing:
Baskerville:
serif: Baskerville
fixed: Courier
…
This is obviously much more human-friendly than plists, and, I would say, than JSON. YAML is, in the words of yaml.org, “A human friendly data serialization standard”, and I think that’s a good description.
You might have noticed there that I said that was a YAML representation of the same thing. I could also have written:
{ 'Baskerville': { 'serif': 'Baskerville', 'fixed': 'Courier'…
Yes - that’s the same as the JSON. YAML is flexible, and is in fact a superset of JSON (through serendipity - YAML came first - although the YAML 1.2 spec solidifies JSON as a subset specifically).
When the creators of YAML had a choice they always tried to specify what would be easier to humans, not to machines. In many ways, YAML looks just like a plain text document any human might intuitively sit down and write as a list or outline. To put it the other way around, any plain text document any human might intuitively sit down and write as a list or outline is probably at least pretty close to being valid YAML.
YAML is strongly typed - though you might not guess so from a simple document. Sequences must be decoded as arrays. Mappings must be decoded as dictionaries. Numbers as number objects or literals. Boolean values to a boolean objects or literals. How would you represent a number as a string? In the most obvious way - put quotes around it.
Red flags are perhaps going up about now. You probably think a data representation language that’s designed for humans is a crazy idea; its very flexibility would paradoxically make it hard to edit, and hard to understand. I did too initially. You’d be wrong though. YAML is unambiguous - a rigorous spec means that for any YAML document, there’s one way to parse it - which I think is the property that saves it in this regard.
All this does make it quite complex to parse, yes (this is perhaps the major criticism of YAML), but let’s be honest: in these days where we’re all carrying around supercomputers in our pockets, “quite complex” is a very relative term.
In some ways, I would say YAML is to data representation what Markdown is to text markup (a strange comparison for me to make, since I’m not, to be honest, Markdown’s biggest fan, but it feels true nonetheless).
As an aside, I do find it curious that something that’s easier for humans to ‘parse’ would be harder for computers to parse. Isn’t the human mind mysterious?
The technical appeal
There are three different native ways to archive a Cocoa object nowadays. For simple types, we use plists or JSON. For complex types, we use NSKeyedArchives.
Why are there three representations? Plists and JSON are both simple, ‘straight-line’ representations. There’s no way of encoding repeated copies of the same object, or cycles in the object graph; they can only represent trees. They also can’t encode types that are not native ‘plist types’ or JSON types. Because of these two issues, they fundamentally can’t natively represent arbitrary encoded objects.
NSKeyedArchives, are richer. They can represent arbitrary ObjC classes. They can also encode that the same object is used in different places in the tree, or even in cycles (with some caveats that mean that decoding cycles is quite complex, and, of course, potentially fraught with retain-cycle peril).
If you’re aware of how NSKeyedArchives are implemented, you might argue that I’m wrong, that there’s only one representation - plists. You’d be sort of right; NSKeyedArchives are actually encoded as binary plists, but there’s lots going on in there that isn’t ‘native’ plist, but instead an encoding mapped on to it. Saying that NSKeyedArchives are just plists is like saying that a plist is just UTF-8 text - it’s ‘true’, but it’s not true.
So, enter YAML. As you’ve seen above, simple YAML looks like a simply typed, straight-line representation when you read it - like JSON with a richer syntax. YAML does, though, also have the ability to encode type information explicitly. This type information can specify arbitrary application-defined types. It also has the ability to define ‘anchors’ in a document, and to refer to these anchors as ‘aliases’ later in the document, enabling it to encode arbitrary object graphs.
Incidentally, anchors can be useful in hand-written YAML too. For example, say you had some complex option, string, or regex that you wanted to use throughout a configuration file. With plists or JSON you’d be entering the entire thing every time. With YAML, you’d give its first use an anchor, and then just refer to it later.
So, with the ability to encode types and object graphs, we have the capacity to archive arbitrary Cocoa objects. What’s more, Apple’s already given us an API to do it - the NSCoder protocol, as used by NSKeyedArchiver, and implemented in many classes already.
YACYAML implements NSCoder, as YACYAMLKeyedArchiver and YACYAMLKeyedUnarchiver, and uses that to implement YACYAMLEncodedString and YACYAMLDecode convenience methods on NSObject and NSString (and NSData ).
To illustrate encoding of arbitrary objects, Here’s a YAML representation of a Mac OS X NSButton. Note that I didn’t implement anything special to get this. NSButton already implements NSCoder, so this is just the output of [YACYAMLKeyedArchiver archivedStringWithRootObject:myButton] on a regular NSButton.
&a !NSButton
NSNextResponder:
NSvFlags: 256
NSFrame: '{{20, 20}, {400, 50}}'
NSTag: -1
NSEnabled: y
NSCell: !NSButtonCell
NSCellFlags: 67239424
NSCellFlags2: 134217728
NSContents: If you wanna come to my house, then click me with your mouse.
NSSupport: &b !NSFont
NSName: LucidaGrande
NSSize: 12
NSfFlags: 4880
NSControlView: *a
NSButtonFlags: -1232977665
NSButtonFlags2: 2
NSAlternateImage: *b
NSKeyEquivalent: ''
NSPeriodicDelay: 400
NSPeriodicInterval: 75
In conclusion
So, there we have it. YACYAML. An excursion prompted by the need to write a small config file that’s resulted in an new Cocoa serialisation format. I’m pleased with the result though. I hope you are too.
If you’re interested in learning more about YAML, Wikipedia’s YAML entry is pretty good, and the official spec can be found at yaml.org. YACYAML is, as I mentioned, on GitHub, and the YACYAML ReadMe there is a more technical counterpart to this blog post.
iOS' hidden Base64 routines
It’s commonly held that iOS has no built in Base64 routines (a strange omission, if you ask me). Pootling around in the BSD headers today though, I discovered this is not entirely true. There are a couple of functions hidden away in libresolv.dylib. That’s the, err, BIND-9 DNS resolution library… If you’re not put off by linking to BIND just to get Base64 translation, it’s easy to use. Here’s the interface (publicly declared in a less readable fashion in resolv.h):
// To encode:
//
// Returns the byte length of the ASCII Base64 encoded data, or -1 on failure.
// Encoded data will be nul-terminated, but the nul-termination byte is not
// included in returned byte length (i.e. it's like a length returned by
// strlen()).
//
// Arguments:
// data: Data to encode.
// datalength: Byte length of data to encode.
// encoded: Buffer to hold encoded data (must be large enough, including
// space for a trailing nul-termination byte).
// encodedsize: Byte capacity of 'encoded' buffer.
//
int b64_ntop(uint8_t const *data, size_t datalength,
char *encoded, size_t encodedsize);
// To decode:
//
// Returns byte length of decoded data, or -1 on failure
//
// encoded: Base64 data to decode. Must be nul-terminated.
// unencoded: Buffer to hold decoded data (must be large enough!).
// unencodedsize: Byte capacity of 'unencoded' buffer.
//
int b64_pton(char const *encoded, uint8_t *unencoded, size_t unencodedsize)
Semi-pseudocode examples (typed direct, so you might want to double-check my math if you’re going to use this!):
#import <resolv.h>
// To encode:
NSData *dataToEncode = /* Data to encode */;
NSData *encodedData = nil;
NSUInteger dataToEncodeLength = dataToEncode.length;
// Last +1 below to accommodate trailing '\0':
NSUInteger encodedBufferLength = ((dataToEncodeLength + 2) / 3) * 4 + 1;
char *encodedBuffer = malloc(encodedBufferLength);
int encodedRealLength = b64_ntop(dataToEncode.bytes, dataToEncodeLength,
encodedBuffer, encodedBufferLength);
if(encodedRealLength >= 0) {
// In real life, you might not want the nul-termination byte, so you
// might not want the '+ 1'.
encodedData = [NSData dataWithBytesNoCopy:encodedBuffer
length:encodedRealLength + 1
freeWhenDone:YES];
} else {
free(encodedBuffer);
}
#import <resolv.h>
// To decode:
// Assuming dataToDecode's nul-terminated. In real life, you might need to copy
// out the data and nul-terminate it yourself :-(.
NSData *dataToDecode = /* Data to decode */;
NSData *decodedData = nil;
NSUInteger decodedBufferLength = dataToDecode.length * 3 / 4;
uint8_t* decodedBuffer = malloc(decodedBufferLength);
int decodedBufferRealLength = b64_pton(dataToDecode.bytes,
decodedBuffer,
decodedBufferLength);
if(decodedBufferRealLength >= 0) {
decodedData = [NSData dataWithBytesNoCopy:decodedBuffer
length:decodedBufferRealLength
freeWhenDone:YES];
} else {
free(convertedBuffer);
}
Remember to make your targets link to libresolv.dylib to avoid cryptic link-time errors.
Please do learn how software works. If you want to.
By all means, learn enough programming to put together a prototype and have a better perspective on hiring and managing engineers. Just don’t mistake a foothold in the world of coding for true engineering expertise.
This “Don’t Learn to Code” vs. “Everyone Should Learn to Code” meme fascinates me. I pretty much agree with everything Buzz says.
I think I’d also add, though, that I find the dichotomy that seems to have developed very strange. I don’t think that anyone is suggesting that everyone must learn all there is to know about programming, or the opposite; that anyone should wilfully disregard it if it interests them. A lot of the arguments online seem to be arguing against one or the other of these extreme positions.
I wonder if it’s because the phrase itself - “learn to code” - is so absolute. People might “learn some plumbing”, but I don’t think that even master-plumbers would talk how they’ve finished “learning to plumb”. I consider myself skilled in the coding art, but I know I haven’t finished “learning to code”, and I never will. Maybe if we replaced “learn to code” with “learn some programming” - or even “learn about how software works” - we’d have a better - or at least clearer - argument about it.
To expand on my own position, I think that most everyone’s life would be easier, or at least richer, if they knew a little about how software works. I’d say the same about car engines too, yet I haven’t made time to learn very much about engine mechanics myself.
Apple, failure, and perfect cookies
It’s painful, hard, and often time-consuming to restart when you’re already done, but you can’t argue with the results. Both Apple and Nintendo create some of the best, most inspired design out there.
When people ask me what Apple’s dev process has that’s different to other places, I always gravitate towards this, even though it sometimes seems ludicrous.
In my time working there, I must personally have seen years-worth, probably decades-worth (and, from afar perhaps even centuries-worth) of work simply discarded because it turned out not to be ‘right’ or ‘good’. This was done with very little animosity towards the people who did the work. There was a distinct difference between working on something that turned out bad and had to be discarded (fine - admirable, even) and doing bad work (bad).
Of course, no-one should set out to fail, that would clearly be ridiculous. Apple doesn’t either, but it does accept that things don’t always turn out as great as they were initially imagined; that’s just how a creative process works.
I think this highlights two things that many other organisations would do well to learn. First, what you have is what it is, it’s not the effort that was put into it. If it’s not worth keeping, it’s not worth keeping. Second, if you want the best results, you need to give good people the room to start over without feeling like they are failing.
My wife has a reputation for baking excellent cookies. When she takes them to friends, she gets congratulated on the even size, roundness and texture of them. When asked how she does it, the reply is always the same - “there’s a selection step you didn’t see”. That step is us eating all the outliers at home*. Strangely, knowing this doesn’t take away from the fact that the cookies now at the friend’s house are perfect. So it is with Apple and its cookies.
*My favourite step!
Easy Xcode static library subprojects and submodules
In which a method is presented for reliably building static libraries with subprojects in Xcode, and it is suggested that this method, combined with Git submodules or other similar mechanisms, provides the best way to share libraries, frameworks, or other code between projects.
I’m sure I don’t need to tell you that it’s a useful thing to be able to share code between projects. On the small end of the scale, you might have created some nice views, or text processing classes, and want to be able to include them in multiple apps. On the larger end, perhaps you produce games written with a shared framework, or you’re maintaining an open source library.
One way to do this is to copy files - or even just snippets of code - between your projects. At a very small scale this works, but as things get larger, inevitably, over time, it leads to a codebase that’s hard to maintain, and probably buggy, because it’s nigh on impossible to keep all the copypasta in sync.
It would be better to keep the shared source together in one place. Doing this, you ensure that bug fixes easily propagate to all the apps using the code, and you can keep tests etc. in a central location.
For iOS (and I would argue, Mac, although I’m focusing on iOS in this post) the ideal way to do this is to create an Xcode project that builds a static library, then include that as a subproject in your app projects. Using features like Git’s submodules, or Subversion’s externals, you can ensure that the files for the same subproject in every app that uses it are kept in sync, with one master copy of the subproject kept centrally. This also enables easier collaboration - whether it’s with colleagues in your company, or the app-developing-public at large via GitHub.
I use this method with libEucalyptus to allow it to be included in multiple apps, some of which I don’t control, but keep it in a central location, and it works well.
“Someone on StackOverflow told me this could never work work.
Or, wait…, maybe they said it always worked automatically?…”
There’s a lot of confusion - and misinformation - out there on the web about how to do this. Most people seem to think that creating a static library project in Xcode, and using this as a subproject in other projects, is fraught with peril - a path filled with custom project configurations, header path manipulation, and strange project-editor voodoo to get things to work. Xcode 4 actually provides fairly good support for doing this though. Set things up correctly once, and you’ll be good to go.
By the end of this blog post, you’ll know how to create an Xcode project that compiles a static library, exposing public headers to other projects in a way that makes them easy to #import, and how to use that project as a subproject in other projects.
You’ll also be able to include this project in superprojects as a submodule using Git, or, if you’re not a Git user, it’ll hopefully be fairly obvious how you could use your favourite version control system to accomplish something similar (for example, using SVN externals).
Create an Xcode project for the static library
Let’s get started making our static library. First, we’ll fire up Xcode and create a new project:

We’ll use the Cocoa Touch Static Library template:

Next, we get to choose a name for our library. Let’s go with the imaginative name of SampleSubproject:

After this, you’ll get a file dialog allowing you to choose where to put the project. I also let Xcode create a Git repository for me - there’s a checkbox in the dialog for that - we’ll use that later.
Hurray, a static library project!

Configure the static library target’s header location
Now, there’s basically only one thing to do to get this ready for other projects to use. By default, Xcode will place your public library headers in place where superprojects can include them like this: #import <HeaderFromTheSubproject.h>. This strikes me as a little messy. I like to change things so that I can include the headers like this: #import <SampleSubproject/HeaderFromTheSubproject.h> - just as you would when using a header from a framework (or, indeed, from many system-provided dynamic libraries). Xcode’s default configuration will also treat the library headers as ‘part of’ your finished product, and when you do an Archive build they’ll be included in the Archive. This is obviously wrong for an iOS app, and besides being messy will also cause your app to fail verification - not desirable.
To fix these problems, we’ll need to change one setting in the project editor. Select the project in the Navigator sidebar, then select the SampleSubproject library target, and make sure that All, not Basic, is selected at the top of the list of settings. Then, in the Packaging section, find the setting entitled Public Header Folders Path. Change it, in the target’s column, to read include/$(TARGET_NAME). This will make Xcode place the headers in a subfolder named after the target, and the lack of a ‘/’ at the start of the path means they’ll be placed next to the library, rather than in what Xcode sees as being a location on the target system, so they won’t be included in the products of Archive builds.

That’s it. Before we go on to setting up an app project to use this subproject though, let’s make the library do something so we can see it working later.
Write the static library code
First, let’s make a simple class to use - we’ll call it “SSHelloer”. It will, in fine sample code tradition, return strings with a “hello” message. I won’t walk you through adding a class to an Xcode project, since I’m sure you know how to do it already, but here’s the code:
#import <Foundation/Foundation.h>
@interface SSHelloer : NSObject
- (NSString *)hello;
@end
#import <SSHelloer.h>
@implementation SSHelloer
- (NSString *)hello
{
return @"Hello from SSHelloer in SampleSubproject!";
}
@end
Create a global header for the static library
Next, it’s a bit odd, really, that the template sets us up a class called SampleSubproject. Usually we’d expect a statement like #import <SampleSubproject/SampleSubproject.h> to import all the headers from the SampleSubproject library, not a SampleSubproject class. Let’s rectify this. Delete the SampleSubproject.m file entirely, and change the interface in SampleSubproject.h file to just #import <SampleSubproject/SSHelloer.h>. It’s not very helpful in our simple single-class library, but in a big project with lots of headers you could list all the headers in a list here, and then clients could just #import <SampleSubproject/SampleSubproject.h> to get everything from the library.

Set the visibility for the library headers
The last thing to do is to set the visibility of the headers in the project. For each header, there are three options. Public means that clients of the library will be able to see the header. This is what we want for headers that we’re intending to be visible to the apps including our project as a subproject. Project means that only files in this project will be able to see the headers. It’s useful for classes that are intended to be available to the library you’re creating, but not exposed to clients of the library. Private is a little misleading. Intuitively, you might think its a synonym for Project, but it’s not. It’s intended for headers that are intended for some clients of the library, but are not public. It’s useful, for example, for Apple, which can have headers for private APIs that are available at build-time for ‘internal’ clients to use, but will not be shipped out to us in Mac OS X SDKs. It doesn’t really make any sense in our situation, so it’s best to just ignore that it’s there, and choose from Public or Package.
From that explanation, you can hopefully see that both our headers, SampleSubproject.h and SSHelloer.h need to be Public. You can set this in the “Target Membership” section of the right-hand “Utilities” panel:

Library subproject ready!
Lastly, just as a sanity check, build the library, and all should be well.
That’s it, our subproject is ready to go. At this point, I committed everything to Git, and pushed it to GitHub at http://github.com/th-in-gs/SampleSubproject. I’ll use the repo from GitHub later to clone it into the app that’s using it. If you want to do something similar, even if you’re using Git, it’s not necessary to use GitHub. You can use submodules from any Git host - or even the local filesystem. You could also use another version control system that supports submodules or externals, or even just filesystem symlinks if you like to live dangerously.
Using the subproject
Well, that’s us with a nice static library set up, we’re home and dry, right? Well, not quite - we’re still not using it. As with setting it up, it’s fairly simple in Xcode 4, but it does take a few steps.
Create an app superproject
First, we create a new single-view app, called, imaginatively again, SampleSuperproject. We’ll set this app’s project up to refer to our SampleSubproject, building and linking the library from it into the SampleSuperproject app when the app is built. When it’s run, it’ll use some of the SampleSubproject library’s wonderful helloing functionality.

Clone the static library into the app superproject’s directory tree
After creating the SampleSuperproject, we need to get the SampleSubproject ‘into’ it somehow. I use Git submodules to do this, but as I mentioned, you could use e.g. Subversion externals or whatever your version control system’s similar alternative is. If you’re not using Git, skip on to the next section.
To include the submodule, make sure your SampleSuperproject is a Git repository (mine already is, since I let Xcode create one for me). We’re going to have to drop down to the terminal to get the submodule into place. Commands I typed are in bold:
lappy8086:SampleSuperproject jamie$ cd SampleSuperproject
lappy8086:SampleSuperproject jamie$ git submodule add git://github.com/th-in-gs/SampleSubproject.git
Cloning into 'SampleSubproject'...
remote: Counting objects: 21, done.
remote: Compressing objects: 100% (14/14), done.
remote: Total 21 (delta 6), reused 21 (delta 6)
Receiving objects: 100% (21/21), 4.15 KiB, done.
Resolving deltas: 100% (6/6), done.
And with that the subproject is set up in the filesystem. Git submodules are slightly strange beasts - the submodule is treated like a single file in the superproject by Git; when you commit it, you commit a reference to a specific version of the subproject. In the filesystem though, the submodule is fully checked out for you to use. If you’re going to be using a subproject from a Git submodule like this, I highly recommend you read up on Git submodules before you do it in earnest, because, like everything with Git, they’re not entirely intuitive. Pro Git has a good chapter on them, as does the official Git Community Book - although I do think that the official examples are rather complex for an introduction. Anyway, for the purposes of this article, let’s assume we now have a copy of the subproject in a directory inside the superproject’s directory.
Add the static library’s .xcodeproj to the app’s project
Getting the subproject into the superproject’s Xcode project is easy. Just find the SampleSubproject.xcodeproj from the subproject folder in Finder, and drag it into Xcode’s Navigator tree. Alternatively, add it with Xcode’s Add Files File menu item (make sure to add the SampleSubproject.xcodeproj file only, not the entire directory).
After you’ve added the subproject, it’ll appear below the main project in Xcode’s Navigator tree:

Configure the app target to build the static library target
Now, we need to get the SampleSuperproject to build and link to the SampleSubproject library.
First, in the SampleSuperproject app’s target settings, find the Build Phases section. This is where we’ll configure the SampleSuperproject target to automatically build and link to the SampleSubproject library.

Once you’ve found that, open the Target Dependencies block and click the ‘+’ button. In the hierarchy presented to you, the SampleSubproject target from the SampleSubproject project should be listed. Select it and click Add.

Configure the app target to link to the static library target
Next, we need to set the app to link to the library when it’s built - just like you would a system framework you wanted to use. Open the Link Binary With Libraries section a bit below the Target Dependencies section, and hit ‘+’ in there too. At the top of the list should be the libSampleSubproject.a static library that the SampleSubproject target produces. Choose it and hit add.

Lastly, because we’re using Objective-C, we’ll have to add a couple of linker flags to the SampleSuperproject app’s target to ensure that ObjC static libraries like ours are linked correctly. In the SampleSuperproject target’s Build Settings, find the Other Linker Flags line, and add -ObjC and -all_load. I won’t go in to what makes these extra flags necessary technically here, I’ll just say that if you don’t want at the very least strange runtime errors when you try to use categories defined in static libraries, you’ll need them.

We’re almost done! If you hit build now, you’ll see that the SampleSubproject library is built before SampleSuperproject app, and they’re linked together. We haven’t actually used any functionality yet though. In order to do this, there’s one more setting to change - we need to be able to find our library’s public headers. Now, there are some sources on the web that will tell you that Xcode 4 will find these automatically, but I’ve never found that to be true.
Configure the app target to use the static library’s headers when building
Again in the SampleSuperproject target’s Build Settings, find the Header Search Paths line this time, and add two settings: "$(TARGET_BUILD_DIR)/usr/local/lib/include" and "$(OBJROOT)/UninstalledProducts/include". Make sure to include the quotes here - they’re necessary if you have any directories with names containing spaces in your file hierarchy (and make sure they’re regular ‘straight’ quotes, not the fancy things Habari will surely turn them into in this post). Why two paths? The first is the ‘normal’ path, where Xcode puts the headers during a normal build. The second is necessary to make builds specifically triggered by the Archive item in the Project menu work. I’m not sure why Xcode’s internal build procedure seems to vary based on whether you’re doing an Archive build or not - to be honest, it feels like a build system bug to me. If anyone knows, I’d love to hear more about it in the comments.

Use the static library in the app
Thats it for the settings! Now, in files compiled by the app target, you can just #import <SampleSubproject/SampleSubproject.h> and use functionality from the library, just as you would from a system framework. In our contrived example, we can call [[[SSHelloer alloc] init] hello]; and get our string back:
![Calling [[[SSHelloer alloc] init] hello];](images/CallingTheLibrarySmall.png)
Rather than walk through changing the project to do this, I’ll assume that you already know how do do things like make labels in Interface Builder and set up a simple UI. If you want to run the sample app, it’s also on GitHub. Remember, it uses Git submodules to include the static library subproject - you need to remember that when cloning it and make sure to also initialise and update the submodules (you did read those Git submodule references I mentioned earlier, right?), like this:
lappy8086:tmp jamie$ git clone git://github.com/th-in-gs/SampleSuperproject.git
Cloning into 'SampleSuperproject'...
remote: Counting objects: 36, done.
remote: Compressing objects: 100% (21/21), done.
remote: Total 36 (delta 13), reused 36 (delta 13)
Receiving objects: 100% (36/36), 9.91 KiB, done.
Resolving deltas: 100% (13/13), done.
lappy8086:tmp jamie$ cd SampleSuperproject/
lappy8086:SampleSuperproject jamie$ git submodule init
Submodule 'SampleSubproject' (git://github.com/th-in-gs/SampleSubproject.git) registered for path 'SampleSubproject'
lappy8086:SampleSuperproject jamie$ git submodule update
Cloning into 'SampleSubproject'...
remote: Counting objects: 25, done.
remote: Compressing objects: 100% (17/17), done.
remote: Total 25 (delta 7), reused 25 (delta 7)
Receiving objects: 100% (25/25), 4.84 KiB, done.
Resolving deltas: 100% (7/7), done.
Submodule path 'SampleSubproject': checked out '8a6ac6af716213b91a3714ce7d6039e121b4b610'
And here’s how it looks:
![Calling [[[SSHelloer alloc] init] hello];](images/OuptutSmall.png)
Recapping
The steps again, in order:
- Create an Xcode project for the static library
- Configure the static library target’s header location
- Write the static library code
- Create a global header for the static library
- Set the visibility for the library headers
- Create an app superproject
- Clone the static library into the app superproject’s directory tree
- Add the static library’s .xcodeproj to the app’s project
- Configure the app target to build the static library target
- Configure the app target to link to the static library target
- Configure the app target to use the static library’s headers when building
- Use the static library in the app
Of course, in reality, you wouldn’t perform these steps in this order. Perhaps you already have app projects, and want to create the subproject to factor out some common code. Or perhaps there’s a library you want to use already that you just need to reconfigure the project for so that you can include it in this way. Hopefully though, now that you’ve seen that it’s not all that hard to get working, you’ll be able to start using library submodules and reap the code-sharing benefits.
I’d love to see more open source projects having static library targets that are set up in the way described above. That way, they’d be ready for app projects to just use directly as submodules without having to keep special, slightly forked, versions of the projects - or worse, but sadly common, including the code directly.
In case you missed the links, here’s the sample subproject on GitHub, along with the sample superproject app, referencing the subproject as a submodule.
On belonging, nostalgia, and British cohesion

This week, I’ve been attending NSConference. It’s a wonderful event. People like me - by which I mean people who enjoy doing and are passionate about similar things to the things I enjoy doing and am passionate about - from all over the world congregate in a conference centre near Reading, England for three days. We talk about these things we enjoy doing, and are passionate about. It takes place in a soulless modern ‘conference venue’, miles from anywhere; the sort of place any sane person would usually find depressing. That’s really part of its genius, though: attendees are …
On Frequent, Intense Mature and Suggestive Themes
Marco Arment has written a provoking article about Apple App store ratings. I haven’t really looked at the situation since my original (and, at the time, infamous) brush with the Apple law over the rating of Eucalyptus. As you may remember, it’s rated, at Apple’s request, 17+ for “Frequent/Intense Mature/Suggestive Themes”, because users can access Victorian textual translations of, for example, the Kama Sutra from it. How are other book reading apps rated? I took a sample of the popular ones:
- iBooks: 4+
- Kindle: 4+
- Blio: 4+
- Nook: 4+
- Stanza: 4+
- Wattpad: 9+
- Kobo: 12+
In case you’re in any doubt about the implications of this (perhaps, for example, the readers have internal rating systems that prevent viewing of ‘inappropriate’ content), this screenshot is from iBooks running on an iPhone with all parental restrictions set to the minimum (so, e.g. 4+):

There are 39 results in that list.
I’ll leave the commentary up to Marco.
On pushing, and the Apple Developer Forums
I posted a question to the Apple Developer Forums today. This is almost always an exercise in frustration, and I think I have an idea why. It’s because web forums are inherently a ‘pull’ service.
It used to be Apple ran developer mailing lists (and they still do - Cocoa Dev, for example, is still thriving), but they’re now pushing people to use the developer forums instead, and not creating mailing lists for new technologies.
A mailing list is easy to ‘hang out’ on - just sign up, and the emails are ‘pushed’ to you. Normal human compulsions then engage, …
I believe in Sherlock Holmes
This graffiti is still up on the bottom of the Dean Bridge. It’s been there for longer than most graffiti survives. You can see how scrubbed the bottom of the bridge ‘underneath’ the bridge is - that’s from repeated swift graffiti removals by the council in the past.
Despite my better intentions - I know that it’s really the defacing of a historic monument - I can’t help but smile every time I run past. For a moment I believe that I’m part of the world of Sherlock, and that I also am one of a small band of …
Slides from “An iOS developer’s OpenGL primer”
In October 2011 I gave a talk at the inaugural NSScotland conference in Edinburgh entitled “An iOS Developer’s OpenGL Primer”. It was an attempt to introduce iOS developers to the world of OpenGL by starting with what must be happening - pixels being displayed on screen - and working up through the layers of abstraction to UIKit. I wanted not to “teach OpenGL” (that could take up a lecture series…), but to make OpenGL seem understandable by explaining away the magic.
I promised then to put the slides online and, almost half a year - and one iOS release - later, I still hadn’t done it. Truth be told, I was actually getting a little chagrined about my blog’s design, and that was going to be the first article published after I reworked it - it took a bit longer than I intended though.
Anyway, excuses aside, here are the slides. I hope someone finds them useful. They even feature three bonus slides that were not in the presentation, on primitive GLSL types. Exciting.
A word of warning: the slides are designed as an aid to the talk, not the other way around. There’s lots of information that was in the talk that’s not on the slides, and that might make them confusing or hard to follow. If there was video of the talk, that would be much better than slides, but there’s not, so slides it is.
The code for the demos is on GitHub. Each of the demos corresponds to a tag - the first demo is step1, the second is step2 etc, so after your git clone https://github.com/th-in-gs/Colours.git you can, for example, git checkout stage2 to try out (and see the code for) the second demo.
If you don’t want to view the sides inline here, or you want to save them for later, you can Download the PDF.
Finally, another word of thanks to Alan C. Francis for organising NSScotland. It was a great day, well worth attending should another one happen this year - I hope one does.
