About the Builder Pattern in Objective-C

When dealing with complex object creation in an iOS application you may feel the need for a cleaner and more fluent approach.

During my latest development my colleague @alexiskinsella set up a pattern based on providers in order to mimic the factory behaviour we often find in languages such as Java.

Recently, in the occasion of a side project, I am studying a slightly different approach, based on this article by Matthias Wessendorf.

What I am willing to do is an image builder which creates UIImages on which a GPUImageFilter is applied.

The result I came to is as follows:

The Builder itself (implementation)

The Configuration object (header)

 Using a Builder with an inline Configuration

This should make for a quite readable code with a reduced number of lines of code in the method that invokes the builder.

How to add custom environment variables in Xcode and CocoaPods

Let’s say we would like to share some Xcode environment variables across multiple targets in a project configured with CocoaPods. For instance, we would to use the same app version number across our development and distribution targets in order not to retype the same values for each version bump over and over again.

The solution I opted for was to:

1. Add a custom xcconfig file (for instance “SharedSettings.xcconfig”) from the Xcode menu

Screen Shot 2013-05-21 at 12.16.11 PM

2. In the project settings, change the configuration settings file to “SharedSettings”

Screen Shot 2013-05-21 at 12.18.43 PM

3. In settings file, in order to preserve your pod configuration, add

#include "Pods/Pods.xcconfig"
VERSION_NUMBER = 3.0

And it’s done: now you can use the environment variable $(VERSION_NUMBER), even in your plist file, without breaking the compatibility with CocoaPods. As a result, any future “pod install” will not break the configuration you have just created.

How to resolve “Unable to start status bar server. Failed to check into com.apple.UIKit.statusbarserver: unknown error code” exception

Ever wondered how to resolve the following error while executing GHUnitTest by Command Line?

*** Assertion failure in -[UIStatusBarServerThread main], /SourceCache/UIKit_Sim/UIKit-2372/UIStatusBarServer.m:96
*** Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘Unable to start status bar server. Failed to check into com.apple.UIKit.statusbarserver: unknown error code’

Well, I did.

In my situation, the exception was caused by having an iPhone Simulator session opened while executing the CLI tests. So, in order to resolve the issue, I simply had to add

killall -s "iPhone Simulator" &> /dev/null
if [ $? -eq 0 ]; then
killall -m -KILL "iPhone Simulator"
fi

on top of the run script file (RunTests.sh) which is executed as last “build phase” of the build process.

EDIT: I wrote another post explaining how to solve the error after migrating to XCode 5.