Active Record for Objective C
I really, really want to be balanced in my discussions regarding the iPhone SDK and Cocoa/Objective C. Its been tough. But an ahah just occurred and I feel its important to lay it out there.
http://github.com/aptiva/activerecord/tree/master
Take a look at the instructions for how to use it. The dynamic nature of Objective C, in that it uses a messaging paradigm for calling methods, means that an inheritance hierarchy can provide structure and guts. That’s cool!
Much like dynamic, interpreted languages (Ruby, Python, Asp classic), you can exercise much freedom with how your objects interact, construct themselves, etc. You also get strong typedness and the performance that comes with that.
In this example, by #import(ing) the active record framework… you can
- Create the table in your database
- Create a model class named as the singularized version of your table name
(people -> person)
for a table with:
id as the primary key
firstName as varchar(255)
lastName as varchar(255)
The required code would be:
person.h
@interface Person : ARBase
@property(readwrite, assign) NSString *firstName, *lastName
@end
person.m
@implementation Person
@dynamic firstName, lastName
@end
That’s it.
Now you have active record capabilities
NSArray *people = [Person find:ARFindAll];
Person *person = [people objectAtIndex:1];
NSLog(@"%@ %@", person.firstName, person.lastName);
September 7th, 2008 at 4:14 am
[…] Active Record for Objective C […]