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",

Get MacRuby: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.