Resource Acquisition is Initialisation (RAII) Explained

May 17th, 2012

In the competition to make the worst acronym, RAII probably comes second after HATEOS. Nevertheless, it is an important concept because it allows you to write safer code in C++ — a harsh, unforgiving language that is all too happy to help you shoot yourself in the foot.

This article will explain exception-safety and common pitfalls in C++. As we work out how to avoid these problems, we will accidentally discover RAII. Then, we will finish by defining exactly was RAII is, and where it is already being used.

Read the rest of this entry »

Adventures Playing SWF Files In OpenGL

March 11th, 2012

A quick video about what I got up to on the weekend.

Read the rest of this entry »

When A Café Is Not A Café – A Short Lesson In Unicode Featuring NSString

March 8th, 2012

Let’s start with two exotic strings (console output is in the code comments):

NSString* apples = NSGetFrenchWord();
NSString* oranges = NSGetFrenchWord();
 
NSLog(@"apples == '%@'", apples); 
//apples == 'café'
NSLog(@"oranges == '%@'", oranges); 
//oranges == 'café'

They look identical, but looks can be deceiving.

NSLog(@"isEqual? %@", [apples isEqual:oranges] ? @"YES" : @"NO");
//isEqual? NO
NSLog(@"[apples length] == %lu", [apples length]);
//[apples length] == 4
NSLog(@"[oranges length] == %lu", [oranges length]);
//[oranges length] == 5

Read the rest of this entry »

Gotchas With Grand Central Dispatch (libdispatch) And Blocks

March 2nd, 2012

GCD is a nice replacement for the old performSelectorInBackground:withObject: and performSelectorOnMainThread:withObject:waitUntilDone: methods and NSOperation. It’s also a nice supplement to NSThread.

However, I think it was over-hyped a little bit by Apple when it was first released. You probably have all these random deadlocks and race conditions and stuff whenever you use multiple threads, but GCD is soooooo good that you don’t have to worry about that anymore. Just use these magic blocks.

The problem is that concurrent programming is notoriously hard to get right. Maybe GCD helps to make it easier, but It doesn’t solve all of your problems. In fact, GCD and blocks introduce some problems of their own. This article will focus on some of those problems.
Read the rest of this entry »

Const Correctness For NSString (And Pointers In General)

December 16th, 2011

So you’re implementing a new notification and you want the name to be a constant. Easy, right?

const NSString* VTMyNewNotification;

If that’s how you do constants, you’re not doing it quite right. Try assign a new value to the alleged constant and watch in horror as the compiler doesn’t stop you.

This is because when you type const NSString*, the compiler interprets that as a pointer to a constant NSString. NSString is already an immutable object, so making a constant NSString doesn’t do anything except maybe cause some compiler errors/warnings later when you try to use it. What you’re really after is a constant pointer to an NSString. It’s ever so subtly different, and written like so:

NSString* const VTMyNewNotification;

Don’t feel bad. It’s a common mistake. I used to do it until Rob Napier schooled me, and now I’m passin’ on the learnin’ to you.

How To Set Up A Secure Git Server At Home (OSX)

December 3rd, 2010

In this article I’m going to show you, step by step, how to set up an OSX machine to provide secure access to git repositories over the internet via ssh. This was tested on OSX 10.6.

github provides git repository hosting with a lovely interface. If github isn’t feasible, then this article will help you set up something similar, unfortunately without the nice interface.

I assume that you already have git installed. If not, install the latest version from the git website.

We will be walking through the following steps:

  1. Give the server a static IP address on the local network
  2. Set up port forwarding on the router
  3. Getting dynamic DNS
  4. Add a user named "git" to the server
  5. Setting up ssh securely on the client computers
  6. Setting up ssh securely on the server
  7. Making a bare git repository
  8. Using your new git server

Read the rest of this entry »

Using Blocks (i.e. Closures) To Improve Transactional Code

November 21st, 2010

I stumbled across a nice article by Jonathan Dann about using blocks to improve transactional code.

When I say "transactional" code, I mean code that has an opening, a middle, and a closing, where the opening and the closing have to be matched. You see it in a few places in Cocoa, such as:

  • KVO:
    1. willChangeValueForKey:
    2. change the value
    3. didChangeValueForKey:
  • NSGraphicsContext:
    1. saveGraphicsState
    2. do some drawing
    3. resoreGraphicsState
  • NSFileWrapper:
    1. open a file
    2. read/write to it
    3. close the file

I’m going to show you a "before and after" comparison of a function that uses NSGraphicsContext and NSAffineTransformation.
Read the rest of this entry »

How Cocoa Bindings Work (via KVC and KVO)

November 5th, 2010

Cocoa bindings can be a little confusing, especially to newcomers. Once you have an understanding of the underlying concepts, bindings aren’t too hard. In this article, I’m going to explain the concepts behind bindings from the ground up; first explaining Key-Value Coding (KVC), then Key-Value Observing (KVO), and finally explaining how Cocoa bindings are built on top of KVC and KVO.
Read the rest of this entry »

AspectObjectiveC framework 1.0 Release

October 29th, 2010

I’ve (finally) released version 1.0 of AOC. I could go on improving it forever, but it’s good enough to release so I’m going to set it free in the wild. Give it a spin, tell your neighbors, and let me know what you think.

Manual:
http://www.tomdalling.com/aoc_doc_mirror/

API Docs:
http://www.tomdalling.com/aoc_doc_mirror/api/

Source/project:
http://github.com/tomdalling/AspectObjectiveC

Download (framework compiled for i386 and x86_64):
http://github.com/…/AspectObjectiveC.framework-v1.0.zip (direct)
http://github.com/tomdalling/AspectObjectiveC/downloads (github download page)

Manufactoria: A Tile-based Programming Game

May 24th, 2010

What is a “tile-based programming game”? That’s a very good question. I never dreamt of such a thing until I played Manufactoria today. Be warned: if you’re not a programmer you will have great difficulty with this game.

A picture is worth a thousand lines of code, so I’ll show you a screenshot from the game. The following screenshot is an implementation of a bubble sort that operates on a queue of boolean variables.

A bubble sort implemented in Manufactoria

A bubble sort implemented in Manufactoria

It’s an absolutely crazy idea for a game, isn’t it? Let me give you a quick overview of it.

Read the rest of this entry »