Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Objective-C for Rubyists

Objective-C for Rubyists

Objective-C might look like a crazy, complex and verbose language to the seasoned Rubyist. In reality, they share many features. This allows you to use your knowledge of Ruby to quickly get started with Objective-C. In this talk, I give an overview of the syntax and semantics of Objective-C and compare and contrast it with Ruby where appropriate.

Mikael Konutgan

March 01, 2014
Tweet

Other Decks in Programming

Transcript

  1. !4

  2. !5

  3. Objective-C NSNumber *a = [NSNumber numberWithInteger:42]; NSNumber *b = [NSNumber

    numberWithInteger:14]; NSNumber *c = [NSNumber numberWithInteger:73]; NSArray *numbers = [[NSArray alloc] initWithObjects:a, b, c, nil]; ! for (NSUInteger index = 0; index < [numbers count]; index++) { NSLog(@"%lu -> %@", index, [numbers objectAtIndex:index]); } ! [numbers release];
  4. Modern Objective-C NSArray *numbers = @[@42, @14, @73]; ! [numbers

    enumerateObjectsUsingBlock:^(NSNumber *number, NSUInteger idx, BOOL *stop) { NSLog(@"%lu -> %@", idx, number); }];
  5. • A strict superset of C • Object oriented, but

    everything is NOT an object • A dynamic runtime • Static or dynamic typing for objects !11 Objective-C
  6. Modern Objective-C • Object literals for arrays, hashes, numbers! •

    Automatic memory management!! • Enumeration with blocks!!! • Domain specific languages!!!! !12
  7. A strict superset of C Everything that works in C,

    works in Objective-C • Control flow: if, while, for • Primitive types, int, long, char, void *, const char * • The C standard library • UNIX system calls !13
  8. int n = 5; const char *str = "hello, world\n";

    unsigned long len = strlen(str); ! if (len > 0) { int i = 0; while (i < n) { printf("%s", str); i++; } } C
  9. // KMUser.h ! @interface KMUser : NSObject ! @property (copy,

    nonatomic) NSString *name; @property (copy, nonatomic) NSString *lastName; ! @end !21 Objective-C
  10. // KMUser.m ! @implementation KMUser ! @synthesize name = _name;

    @synthesize lastName = _lastName; ! @end !23 Objective-C
  11. // KMUser.h ! @interface KMUser : NSObject ! @property (copy,

    nonatomic) NSString *name; @property (copy, nonatomic) NSString *lastName; ! - (KMUser *)initWithName:(NSString *)name lastName:(NSString *)lastName; ! @end !25 Objective-C
  12. // KMUser.m ! @implementation KMUser ! - (id)initWithName:(NSString *)name lastName:(NSString

    *)lastName { self = [super init]; if (self) { _name = name; _lastName = lastName; } return self; } ! @end !26 Objective-C
  13. The Muffin Man @interface TheMuffinMan : NSObject ! - (BOOL)doYouKnowTheMuffenMan:(TheMuffinMan

    *)theMuffinMan; ! @end ! @implementation TheMuffinMan ! - (BOOL)doYouKnowTheMuffenMan:(TheMuffinMan *)theMuffinMan { return YES; } ! @end
  14. user = User.new("Mikael", "Konutgan") ! name = user.name last_name =

    user.last_name ! user.name = “Kristofer" user.last_name = nil !29 Ruby
  15. KMUser *user = [[KMUser alloc] initWithName:@“Mikael" lastName:@“Konutgan"]; ! NSString *name

    = user.name; NSString *lastName = user.lastName; ! user.name = @"Kristofer"; user.lastName = nil; ! name = [user name]; lastName = [user lastName]; ! [user setName:@“Kristofer"]; [user setName:nil]; !30 Objective-C
  16. Objective-C • Objects are all pointers • But you (almost)

    never need to worry about that • Just don’t forget the star, when declaring variables or parameters
  17. Ruby locations = { 'Mikael' => 'Vienna', 'Kristofer' => 'Istanbul',

    'Lukas' => 'Istanbul' } ! location = locations['Kristofer']
  18. • Objective-C doesn’t have proper namespaces • We use 2

    or 3 letters to prefix all class names to prevent clashes • For example, the Foundation framework that defines strings, arrays, hashes etc. uses “NS” • “NS” stands for “NeXTSTEP” Prefixing classes
  19. Duck typing When I see a bird that walks like

    a duck and swims like a duck and quacks like a duck, I call that bird a duck.
  20. Objective-C - (void)action:(id)sender { if ([sender isKindOfClass:[UIButton class]]) { //

    ... } else if ([sender isKindOfClass:[UITapGestureRecognizer class]]) { // .. } }
  21. Block • Enumeration • Passing code around • Async tasks,

    completion handlers • Blocks are closures
  22. Grand Central Dispatch dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); ! dispatch_async(queue,

    ^ { // Long background task ! dispatch_async(dispatch_get_main_queue(), ^ { // Update the UI }); });
  23. Ruby (Bacon) describe 'A new array' do before do @ary

    = Array.new end it 'should be empty' do @ary.should.be.empty @ary.should.not.include 1 end end
  24. Objective-C (Kiwi) describe(@"Team", ^ { context(@"when newly created", ^ {

    it(@"should have a name", ^ { id team = [Team team]; [[[team name] should] equal:@"Black Hawks"]; }); it(@"should have 11 players", ^ { id team = [Team team]; [[[team should] have:11] players]; }); }); });
  25. rubygems and bundler # Gemfile ! source 'https://rubygems.org' ruby '2.1.0'

    ! gem 'rack' gem 'vienna', '>= 0.4.0' gem 'thin'
  26. Books • Objective-C Programming: The Big Nerd Ranch Guide •

    iOS Programming: The Big Nerd Ranch Guide • iOS 7 Programming Fundamentals • Programming iOS 7