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 »

Why performSelector: Is More Dangerous Than I Thought

May 14th, 2010

I fixed a rather nasty bug today in AspectObjectiveC. One particular unit test would crash with EXC_BAD_ACCESS every time. After learning far more about registers and ABIs than I ever wanted to know (thanks, Greg Parker), it dawned on me that performSelector: was corrupting memory. It was particularly hard to track down because the crash would happen a couple of lines after the call to performSelector:, when the corrupted memory was actually accessed.

I’ve never had a problem with performSelector: before, but this time I was using it a little differently. The return value of the selector was an NSRect.

Now for the gory explanation.

Read the rest of this entry »

For those who have never used Objective C

April 28th, 2010

There is one feature of the Objective C language that I really love: the method naming. Let me explain with an example.

Here is a nasty call to a C function from the Win32 API that has 12 arguments:

hwnd = CreateWindowEx(WS_EX_LAYERED,
                      TEXT("Hello"),
                      TEXT("World"),
                      WS_OVERLAPPEDWINDOW,
                      10, 
                      10,
                      400, 
                      400,
                      NULL, 
                      NULL,
                      hInstance, 
                      NULL);

Pick an argument, any argument. What does it do? You can probably guess a couple of them, but basically you’re forced to look up the documentation. Sure, 12 arguments is a bit excessive, but even three or for argument functions can be ambiguous. What if you’re trying to understand a function call that is being passed three number literals as arguments? Even if you know the function, you’ll probably have to look up the documentation just to remember the order of the arguments.

Now for the equivalent in Objective C:
Read the rest of this entry »

Video: AspectObjectiveC In Five Minutes

March 20th, 2010

Here’s a quick video about AspectObjectiveC, and what it can do. The code is available from github. I should also mention that AspectObjectiveC isn’t ready for release just yet.

Side Project: AspectObjectiveC

March 11th, 2010

I’ve started a new side project called AspectObjectiveC on github. It’s a little aspect-oriented programming framework for objective-c.

In a nutshell, it allows you to run arbitrary code before, after, or instead of any method at runtime. You can modify arguments before they enter the method, modify the return value of the method, or completely replace the method’s implementation without touching the class’ source code. This includes classes that you don’t have the source code for (e.g. other frameworks). It’s released under the MIT license, even though it doesn’t mention it anywhere yet.

It’s still rough, but it’s working (on my machine :) ). If you want to try it out then build the framework target and whack it into an app as a private framework. It’s got no documentation yet, but the public headers only expose one protocol (AOCAdvice) and one class (AOCAspectManager) with two methods, so it’s pretty intuitive. You can check the unit tests for example usage too.

If you want to contribute code, that’d be great. If you want to test it out on platforms other than OSX >= 10.5, i386 with gcc, that would also be great. Feel free to shoot me an email about it.