O'Reilly logo

MacRuby: The Definitive Guide by Matt Aimonetti

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 12. Objective-C Code in MacRuby Apps

In the previous chapter, you saw how to embed MacRuby into an Objective-C project. While MacRuby works great for building Cocoa apps, there are some cases where performance and low-level programming is important. In other cases, you may already have a library written in Objective-C and you would like to use it in your MacRuby project.

Dynamic Library

If the Objective-C code you would like to use isn’t in a framework, the easiest way to package it to make use of it in MacRuby is to create a Dynamic Library.

Let’s pretend we already wrote some awesome Objective-C code that we rely on heavily, and we want to share this code between our projects. Following is the code we are going to try to reuse.

Header file—Spelling.h:

#import <Foundation/Foundation.h>

@interface Spelling : NSObject{
  NSDictionary *table;
}

- (Spelling*) initWithBuiltinTable;
- (NSString*) britishize:(NSString*)string;
@end

Implementation file—Spelling.m:

#import "Spelling.h"

@implementation Spelling

- (Spelling*) initWithBuiltinTable {
  self = [super init];
  if ( self ){
    table = [NSDictionary dictionaryWithObjectsAndKeys:
      @"flat", @"apartment",
      @"row", @"argument",
      @"pram", @"baby carriage",
      @"plaster", @"band-aid",
      @"loo", @"bathroom",
      @"tin", @"can",
      @"mince", @"chopped beef",
      @"biscuit", @"cookie",
      @"maize", @"corn",
      @"nappy", @"diaper",
      @"lift", @"elevator",
      @"rubber", @"eraser",
      @"torch", @"flashlight",
      @"chips", @"fries",
      @"petrol", @"gas",
      @"bloke", @"guy",
      @"motorway",

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required