Ole Begemann: iOS Development

iOS SDK, Cocoa and Objective-C

If you want to learn Cocoa…

with 2 comments

…be prepared to write a lot more code than in your typical “scripting” language. Three examples (I am using Ruby for comparison because it is the scripting language I am most familiar with; things would look very much the same with Python or PHP):

1. Declare a small hash/dictionary of key-value pairs

Ruby:

person = { 
  :name           => "John Appleseed", 
  :age            => 30, 
  :favorite_fruit => [ "apple", "banana", "kiwi" ]
}

Cocoa:

NSDictionary *person = [NSDictionary dictionaryWithObjectsAndKeys:
                        @"John Appleseed",               @"name",
                        [NSNumber numberWithInteger:30], @"age",
                        [NSArray arrayWithObjects:@"apple", @"banana", @"kiwi", nil],
                                                         @"favoriteFruit",
                        nil];

Now which of these is easier to read?

2. What day of the month is today?

Ruby:

today = Date.today                       # => #<Date: 4910525/2,0,2299161>
puts "Day of the month: %d" % today.day  # => Day of the month: 7

Cocoa:

NSDate *today = [NSDate date];                        // => 2010-03-07 15:15:26 +0100
NSCalendar *calendar = [NSCalendar currentCalendar];  // => <__NSCFCalendar: 0x100400160>
NSDateComponents *components = [calendar components:NSDayCalendarUnit
                                           fromDate:today];
NSLog(@"Day of the month: %d", [components day]);     // => Day of the month: 7

3. Fetch something from the data store

Ruby on Rails (Active Record):

result = Person.find(:all, :conditions => { :first_name => "John" }, :order => "created_at DESC")

Cocoa (Core Data):

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
 
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
 
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstName == %@", @"John"];
[fetchRequest setPredicate:predicate];
 
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"creationDate" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[sortDescriptor release];
 
NSError *fetchError = nil;
NSArray *fetchResult = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
[fetchRequest release];

Now I know this is not a fair comparison, Objective-C/Cocoa has many advantages over Ruby et al., you can’t compare Core Data to Active Record, etc. Still, I find the differences very striking, especially regarding the readability of the code.

Written by Ole Begemann

March 7th, 2010 at 3:59 pm

Posted in Uncategorized

2 Responses to 'If you want to learn Cocoa…'

Subscribe to comments with RSS or TrackBack to 'If you want to learn Cocoa…'.

  1. Wow, that was really helpful. That was the most informative thing I’ve ever read! I hope you can do more comparing Perl, C, Haskell, ADA, LISP, FORTRAN, and COBOL, too!

    Thanks!

    I'm impressed

    16 Mar 10 at 12:23 am

  2. ^Previous poster.. don’t be so sarcastic… I really enjoy reading things like this, I tend to learn languages by building upon concepts I’ve learned elsewhere. As I’m familliar with Rails, I enjoy seeing how things are done in Cocoa.

    Due to it being a compiled and memory managed language, it will obviously require more lines of code.. however the verbosity of Cocoa tends to aid it’s readability for me, and I have a pet peeve for strange abbreviations that other languages/frameworks tend to foster.
    Xcode’s autocompletion makes it a non-issue.

    Ryan

    6 Apr 10 at 2:19 pm

Leave a Reply