Dynamic Objective-C for Rubyists

A lot of Rubyists still think Objective-C is the Devil’s language, full of compiler errors and terrifying memory management and other such infernal nonsense. And they’d be right. But what many of them don’t realize is that Objective-C is like your friend’s sexy sister – it’s got all kinds of hot moves, even if it is a little off-putting and the family resemblance a bit unsettling.

I’ll do my best to Enumerate (ha!) the dynamic similarities between the languages. If I’ve missed something, let me know!

Dear Objective-C object: can I send or __send__ you a message?

Why yes you can. Simply call performSelector with up to two extra parameters:

[obj performSelector:@selector(myMethod)];
[obj performSelector:@selector(myMethod) withObject:myParameter];
[obj performSelector:@selector(myMethod) withObject:myParameter withObject:anotherParameter];

You can also get a little funky and mess around with threads and delays. These are especially useful if you want to keep your UI updating (such as updating a progress bar while searching for data). Simply use the following:

[obj performSelector:@selector(myMethod) withObject:myParameter afterDelay:2.0];
Shouldn’t I make sure the object understands the method? You know, Ruby has this respond_to? method…

Wouldn’t you know it, Objective-C has nearly the same method:

SEL dubiousSelector = @selector(dubiousMethod);
if ([obj respondsToSelector:dubiousSelector])
  [obj performSelector:dubiousSelector];
What if I want to construct messages at runtime? I can’t exactly do that with @selector.

Don’t worry – Cocoa provides many convenience functions, including NSSelectorFromString, which will allow you to create messages willy-nilly:

SEL willyNillySelector = NSSelectorFromString(aString);
[obj performSelector:willyNillySelector];
But let’s say I want to get extra freaky with my methods. In Ruby I can use method_missing to catch any message…

Thanks to Ruby and Objective-C’s shared ancestry of the much-lamented SmallTalk language, you can accomplish dynamic message processing in either. It’s a bit trickier in Objective-C (specifically Cocoa), but it can be done by defining forwardInvocation.

And wouldn’t you like to know how it works? I would too! But I need to read a little more about it! So stay tuned, ‘cuz just like the subject matter, this blog is Dynamic! Ha! Get it!

Messages are great and all, but what if I want to mess with method implementations themselves? I can simply define_method it up in Ruby…

No problem – the Objective-C runtime has all kinds of fanciness for messing with class and object innards. Although you can’t yet create closures at a whim, you can still switch around implementations.

Note: you should always import the following header when utilizing runtime functions:

#import </usr/include/objc/objc-class.h>

Some functions you might be interested in for messing around with methods are:

BOOL class_addMethod(Class cls, SEL name, IMP imp, const char *types)
IMP class_replaceMethod(Class cls, SEL name, IMP imp, const char *types)
IMP class_getMethodImplementation(Class cls, SEL name)

They each require some or all following information:

  • The class being modified. To modify instances of the class, you’d pass
    [MyClass class]
    in; to modify the class itself, you’d pass
    [MyClass class]->isa
  • The selector (method name) being updated
  • A Method Implementation (type ‘IMP’), which is just the compiled code that underlies a given method
  • A string (technically a C character array, so don’t prepend an @ symbol to it) containing the types of the method signature. The first character represents the return type (”v” for void, “@” for object references, etc.; those are the most common you’d use, but there’s a list of type encodings you can refer to for more). The second and third should always be “@:”, representing an internal necessity. Any other characters are used to represent the actual argument types.

You probably figured out that the easiest way to define a method implementation (IMP) is to use class_getMethodImplementation. That way, you can do something like the following:

IMP myMethodImplementation = class_getMethodImplementation([MyClass class], @selector(methodWithArg:andArg:));
class_replaceMethod([OtherClass class], @selector(otherMethodWithArg:andArg), myMethodImplementation, "v@:@@");

If that seems a little complex, don’t worry – it’s easy enough to boil it down into a convenience method:

#import </usr/include/objc/objc-class.h>  

void SwizzleMethod(Class destClass, SEL destSelector, Class origClass, SEL origSelector, BOOL isInstanceMethod) {
  Method dMethod = class_getClassMethod(isInstanceMethod ? destClass : destClass->isa, destSelector);
  Method oMethod = class_getClassMethod(isInstanceMethod ? origClass : origClass->isa, origSelector);
  if(!class_addMethod(isInstanceMethod ? destClass : destClass->isa, destSelector, method_getImplementation(oMethod), method_getTypeEncoding(oMethod)))
    method_exchangeImplementations(dMethod, oMethod);
}

// Example instance method usage:
SwizzleMethod([NSString class], @selector(length), [MyClass class], @selector(customLength), YES);

// Example class method usage:
SwizzleMethod([NSString class], @selector(string), [MyClass class], @selector(customCreator), NO);

You can put the SwizzleMethod function into a file and include it globally in your prefix header (usually named something like MyProject_Prefix.pch).

How about extending existing classes? I’d love to add some ActiveSupport-style convenience methods…

First of all, be sure to check out the Objective Support project, which accomplishes a lot of the goodness you’re used to from ActiveSupport.

Objective Support does its magic largely through the use of categories, which are a nice mixin-style feature of Objective-C. Essentially, categories allow you to “re-open” classes just like Ruby; you can create multiple categories for any given class and insert methods as you see fit. You can’t add instance variables, but you can override existing methods.

Thankfully, the implementation of categories is fairly simple:

/* Define the category's interface. In this case, we're
   re-opening NSString and calling our category "Utils" */

@interface NSString (Utils)
- (NSArray*) split;
- (NSArray*) split:(NSString*)delimiter;
@end

// Implement the category's interface
@implementation NSString (Utils)

- (NSArray*) split {
  return [self split:@","];
}

- (NSArray*) split:(NSString*)delimiter {
  return [self componentsSeparatedByString:delimiter];
}

@end

Note: you may want to include your category header in your prefix header (or at least the classes in which its methods get called), as otherwise you’ll see a bunch of “object may not respond to xxxx” warnings.

Neat-o! They should just call it “Dynamic-C”!

This is by no means an exhaustive list of Objective-C’s dynamism, but it covers a lot of the goodies. If there’s something I left off that you earnestly believe should be here, or you see a (gasp!) code error, let me know!


Individually Rounded Corners for your iPhone iPleasure

Everyone likes rounded corners (except Internet Explorer, but who cares?). By now, we’re used to simple declarations to get our hot sexy web 2.0 roundedness in Webkit and Firefox:

border-radius: 10px;

Or if you want to get really freaky, you can define radii individually for each corner (unfortunately this syntax varies by rendering engine):

-webkit-border-top-right-radius: 10px;
-webkit-border-bottom-left-radius: 20px;

Now, if there’s anything I’ve learned about developing for the iPhone, it’s that it takes 10 times as much work as web development. Rounded corners were no exception, until iPhone OS 3.0 released this little gem:

#import <QuartzCore/QuartzCore.h>
CALayer* layer = [myUIView layer];
layer.cornerRadius = 10.0;

Suddenly rounding corners on the iPhone was as easy as slicing bread. Yet there is no way to round individual corners like in a web browser, and much more importantly, using cornerRadius will kill your performance – especially on older devices. Sure, you can get by if you just have one or two rounded views and no scrolling, but we should haven’t to limit the glory of our rounded revolution, now should we?

Because I needed excellent performance with many simultaneous views as well as individually rounded corners, I opted to see what else I could come up with. Luckily, there were several examples on the interwebs with good techniques, though none gave me a simple way to round corners individually. So, after a little sweat, I came up with the following code for you, my dear readers, to use and nuture and love:

CGFunctions.h
//
//  CGFunctions.h
//  OccoTouch
//
//  Created by Mike Laurence on 1/11/10.
//  Copyright 2010 Punkbot LLC. All rights reserved.
//

#pragma mark -
#pragma mark Rounded Corners
void CGContextAddRoundedRect(CGContextRef context, CGRect rect, int inset, int ul, int ur, int lr, int ll);
CGFunctions.m
//
//  CGFunctions.m
//  RoundedCorners
//
//  Created by Mike Laurence on 1/11/10.
//  Copyright 2010 Punkbot LLC. All rights reserved.
//

#import "CGFunctions.h"

#pragma mark -
#pragma mark Rounded Corners

void CGContextAddRoundedRect(CGContextRef context, CGRect rect, int inset, int ul, int ur, int lr, int ll) {

    CGContextSaveGState(context);

    int xLeft = rect.origin.x + inset;
    int xRight = rect.origin.x + rect.size.width - inset;
    int yTop = rect.origin.y + inset;
    int yBottom = rect.origin.y + rect.size.height - inset;

    if (ul > 0) {
        CGContextMoveToPoint(context, xLeft, yTop + ul);
        CGContextAddArcToPoint(context, xLeft, yTop, xLeft + ul, yTop, ul);
    }
    else
        CGContextMoveToPoint(context, xLeft, yTop);

    if (ur > 0) {
        CGContextAddLineToPoint(context, xRight - ur, yTop);
        CGContextAddArcToPoint(context, xRight, yTop, xRight, yTop + ur, ur);
    }
    else
        CGContextAddLineToPoint(context, xRight, yTop);

    if (lr > 0) {
        CGContextAddLineToPoint(context, xRight, yBottom - lr);
        CGContextAddArcToPoint(context, xRight, yBottom, xRight - lr, yBottom, lr);
    }
    else
        CGContextAddLineToPoint(context, xRight, yBottom);

    if (ll > 0) {
        CGContextAddLineToPoint(context, xLeft + ll, yBottom);
        CGContextAddArcToPoint(context, xLeft, yBottom, xLeft, yBottom - ll, ll);
    }
    else
        CGContextAddLineToPoint(context, xLeft, yBottom);

    CGContextClosePath(context);
    CGContextRestoreGState(context);
}

(Full sample project)

This draws a path on your graphics context, which can then be used to draw outlines or fill with colors/patterns/etc.

Notes:

  • Because CGContextAddRoundedRect is defined as a function and not a method of some class, you’ll need to do a little configuration to get your compiler to accept it. Mainly, find your project’s precompiled header file (it should be called something like ProjectName_Prefix.pch) and insert the following line:

    #import "CGFunctions.h"

    That will allow you to use the function anywhere in your code.

  • Watch out for the stroke going outside the view rectangle – you may get weird, uneven-looking drawing. I’ve provided an “inset” parameter in the CGContextAddRoundedRect function so that you can avoid just that. A good rule of thumb is to provide the same value to the inset as you do to your stroke width (you may know it as line width, set via the CGContextSetLineWidth function).

Here’s a simple example of using CGContextAddRoundedRect in practice:

- (void) drawRect:(CGRect)rect {
    // Get graphics context for this view
    CGContextRef context = UIGraphicsGetCurrentContext();
   
    // Set our fill color
    [fillColor set];
   
    // Add the path and fill. Note that our inset parameter (3rd param) is equal to our stroke width.
    CGContextAddRoundedRect(context, rect, strokeWidth, upperLeftRadius, upperRightRadius, lowerRightRadius, lowerLeftRadius);
    CGContextFillPath(context);
   
    // Set our stroke color
    [strokeColor set];
   
    // Set our stroke width
    CGContextSetLineWidth(context, strokeWidth);
   
    // Add the path again and fill
    CGContextAddRoundedRect(context, rect, strokeWidth, upperLeftRadius, upperRightRadius, lowerRightRadius, lowerLeftRadius);
    CGContextStrokePath(context);
}

In addition to painting, you can also use paths to mask images and do all kinds of other fun stuff with Quartz (check out this article for some sample image masking code).

That ought to get you started. Now, go forth and spread the rounded corner love!


Quick, Convert Your RubyCocoa To MacRuby!

You won’t regret it. Really. MacRuby is like filet mignon, while RubyCocoa is just a sad, undercooked rumproast. Did I just paraphrase a joke from American Pie 2? You better believe it, buster.

Anyway, if you have a RubyCocoa project that needs a little bit of shininess, it may be surprisingly easy (and satisfying) to quickly port it over to MacRuby. I just spent about 4 hours converting a fairly sophisticated app (aside from its MIDI interface, which is a bit more tricky, but you probably won’t be dealing with that).

My first stop was this post, a very nice summary of the basic steps that need to be taken. However, I encountered quite a few additional snags, and I thought I’d post them just in case you, random internet denizen, want to skip them all together. Most of them are covered in the MacRuby docs, of course, but they’re here in a nice digestible table format so you can more easily knock out your conversion. Because as fun as prettying up your code is, you’d still rather be adding functionality, right?

Read the rest of this entry »