Objective-C Invented in 1983, adopted by NeXT, currently maintained by Apple Pretty much used only for Apple platforms Statically typed object- oriented language Strict superset of C Preprocessed
RubyMotion Implementation of Ruby 1.9 (sort of) on LLVM and the Objective-C runtime Closed-source compiler, available commercially for $199 (+ annual support fee) Open-source build tools, based on Rubygems and Rake
iOS Developer Agreement 3.3.1 Applications may only use Documented APIs in the manner prescribed by Apple and must not use or call any private APIs. Applications must be originally written in Objective-C, C, C++, or JavaScript as executed by the iPhone OS WebKit engine, and only code written in C, C++, and Objective-C may compile and directly link against the Documented APIs (e.g., Applications that link to Documented APIs through an intermediary translation or compatibility layer or tool are prohibited).
iOS Developer Agreement 3.3.1 Applications may only use Documented APIs in the manner prescribed by Apple and must not use or call any private APIs. Applications must be originally written in Objective-C, C, C++, or JavaScript as executed by the iPhone OS WebKit engine, and only code written in C, C++, and Objective-C may compile and directly link against the Documented APIs (e.g., Applications that link to Documented APIs through an intermediary translation or compatibility layer or tool are prohibited).
class Venue attr_accessor :name, :address, :location def distanceFromLocation(location) self.location.distanceFromLocation(location) end end Everything inherits from (NS)Object
class Venue attr_accessor :name, :address, :location def distanceFromLocation(location) self.location.distanceFromLocation(location) end end Everything inherits from (NS)Object Dynamic/“duck” typing
class Venue attr_accessor :name, :address, :location def distanceFromLocation(location) self.location.distanceFromLocation(location) end end Everything inherits from (NS)Object Dynamic/“duck” typing Implicit return
class Venue attr_accessor :name, :address, :location def distanceFromLocation(location) self.location.distanceFromLocation(location) end end Everything inherits from (NS)Object Dynamic/“duck” typing Implicit return All dot syntax, all the time
class Venue attr_accessor :name, :address, :location def distanceFromLocation(location) self.location.distanceFromLocation(location) end end Everything inherits from (NS)Object Dynamic/“duck” typing Implicit return All dot syntax, all the time No interface file required
@interface DDFood : NSObject { BOOL isPizza; } - (void)setIsPizza:(BOOL)value; @end @implementation DDFood - (void)setIsPizza:(BOOL)value { isPizza = value; } @end class Food def isPizza=(pizzaness) @isPizza = pizzaness end def isPizza! @isPizza = true end def isPizza? @isPizza ||= false end end Instance vars must be declared Anything with an @-sigil is an ivar
// YellingString.rb class String def yell self + "!" end end "Kind of awesome".yell.yell.yell #=> "Kind of awesome!!!" // NSString+Yelling.h @interface NSString (Yelling) - (NSString*) yell; @end // NSString+Yelling.m @implementation NSString (Yelling) - (NSString*) yell { return [self stringByAppendingString:@"!"]; } @end [@"Kind of awesome" yell]; //=> @"Kind of awesome!" Classes extended via categories Classes can be reopened at any time
Ruby has real namespaces NSString, GBFont, DDCoreDataManager MyApp::Venue, MyApp::Item, CoreData::Manager Objective-C class names are prefixed Ruby classes/modules can be nested inside each other
Multiple inheritance via mixins module StaticTableViewController CellInfo = Struct.new(:text, :accessory_type, :action) def tableView(tableView, cellForRowAtIndexPath:indexPath) cell = self.infoForCells[indexPath.row] # Create and return a UITableViewCell end end module SettingsViewController < UITableViewController include StaticTableViewController end
Blocks! class PlaceFinder def self.placeFinderWithBlock(&block) newFinder = self.alloc.init yield newFinder if block_given? return newFinder end end @finder = PlaceFinder.placeFinderWithBlock do |finder| finder.location = CLLocation.alloc.initWithLatitude(lat, longitude:lng) finder.numberOfResults = 10 end
HTTP.get("http://github.com/ddemaree") do |response| p response.body.to_str # prints the response's body end @singleton = nil Dispatch.once do @singleton ||= self.alloc.initWithOptions({}) end Providing a completion callback for a HTTP request Initializing a singleton in a thread-safe way
class PlacesViewController < UITableViewController SECTIONS = [:budget, :lock, :email] def tableView(tableView, numberOfRowsInSection:section) sectionName = SECTIONS[section] if sectionName == :budget return 2 end end def numberOfSectionsInTableView(tableView) SECTIONS.length end end
RubyMotion’s C-like APIs Dispatch.once { @singleton ||= self.new } errorPtr = Pointer.new(:object) ABAddressBookCreateWithOptions(nil, errorPtr) Pointers are objects Functions are wrapped as methods on the Object class Grand Central Dispatch is wrapped as the Dispatch module
alert = UIAlertView.alloc.initWithTitle "Hey, buddy", message: "Buzz off!", delegate: nil, cancelButtonTitle: nil, otherButtonTitles: nil alert.show() App.alert("Hey, buddy", message: "Buzz off!") do |alert| # You can perform any additional configuration on the # UIAlertView object here, using the `alert` variable end BubbleWrap Standard Cocoa API (in RubyMotion)
VS Familiar (if you’re coming from Ruby/open-source development) Lighter-weight, which can be faster / more nimble Have it your way Supported by Apple Designed specifically for Cocoa/Cocoa Touch app development Excellent integrated documentation & code completion
“Using XCode is like driving a very used, modern car - it routinely breaks down, freezes and screws up the rest of my system and there is no way to understand what it wrong because all the parts are hidden from you. For example, it will stop compiling and simply freeze, acting like it's doing something. It will kill other processes and cause them to freeze (terminal processes simply stop getting cycles). It will stop responding to step over, step in, etc actions. “If there is any way for you to avoid XCode, do so. So far, I have had to force quit 4 times since 9:00 AM.”