Slide 1

Slide 1 text

Objective-C for Rubyists

Slide 2

Slide 2 text

Mikael Konutgan iOS Developer, aaa – all about apps GmbH !2

Slide 3

Slide 3 text

Why Objective-C?

Slide 4

Slide 4 text

!4

Slide 5

Slide 5 text

!5

Slide 6

Slide 6 text

A Quick Example

Slide 7

Slide 7 text

Ruby numbers = [42, 14, 73] ! numbers.each_with_index do |number, idx| puts "#{idx} -> #{number}" end

Slide 8

Slide 8 text

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];

Slide 9

Slide 9 text

Modern Objective-C NSArray *numbers = @[@42, @14, @73]; ! [numbers enumerateObjectsUsingBlock:^(NSNumber *number, NSUInteger idx, BOOL *stop) { NSLog(@"%lu -> %@", idx, number); }];

Slide 10

Slide 10 text

Objective-C

Slide 11

Slide 11 text

• 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

Slide 12

Slide 12 text

Modern Objective-C • Object literals for arrays, hashes, numbers! • Automatic memory management!! • Enumeration with blocks!!! • Domain specific languages!!!! !12

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

Booleans BOOL shouldBeginEditing = YES; ! BOOL shouldEndEditing = NO; !15

Slide 16

Slide 16 text

Defining Classes !16

Slide 17

Slide 17 text

# user.rb ! class User end !17 Ruby

Slide 18

Slide 18 text

// KMUser.h ! @interface KMUser : NSObject ! @end !18 Objective-C

Slide 19

Slide 19 text

// KMUser.m ! @implementation KMUser ! @end !19 Objective-C

Slide 20

Slide 20 text

class User attr_accessor :name, :last_name end !20 Ruby

Slide 21

Slide 21 text

// KMUser.h ! @interface KMUser : NSObject ! @property (copy, nonatomic) NSString *name; @property (copy, nonatomic) NSString *lastName; ! @end !21 Objective-C

Slide 22

Slide 22 text

// KMUser.m ! @implementation KMUser ! @end !22 Objective-C

Slide 23

Slide 23 text

// KMUser.m ! @implementation KMUser ! @synthesize name = _name; @synthesize lastName = _lastName; ! @end !23 Objective-C

Slide 24

Slide 24 text

Ruby class User attr_accessor :name, :last_name ! def initialize(name, last_name) @name = name @last_name = last_name end end

Slide 25

Slide 25 text

// 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

Slide 26

Slide 26 text

// 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

Slide 27

Slide 27 text

The Muffin Man @interface TheMuffinMan : NSObject ! - (BOOL)doYouKnowTheMuffenMan:(TheMuffinMan *)theMuffinMan; ! @end ! @implementation TheMuffinMan ! - (BOOL)doYouKnowTheMuffenMan:(TheMuffinMan *)theMuffinMan { return YES; } ! @end

Slide 28

Slide 28 text

Using classes

Slide 29

Slide 29 text

user = User.new("Mikael", "Konutgan") ! name = user.name last_name = user.last_name ! user.name = “Kristofer" user.last_name = nil !29 Ruby

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

Objective-C Messages

Slide 32

Slide 32 text

• initWithName:lastName: ! • replaceObjectsInRange:withObjectsFromArray:range: !32 Selectors

Slide 33

Slide 33 text

Selectors - (KMUser *)initWithName:(NSString *)name lastName:(NSString *)lastName;

Slide 34

Slide 34 text

Ruby person.say_hello("Lukas")

Slide 35

Slide 35 text

Objective-C [person sayHello:@"Lukas"];

Slide 36

Slide 36 text

Dynamic message sending

Slide 37

Slide 37 text

Ruby person.send(:say_hello, "Lukas", "French")

Slide 38

Slide 38 text

Objective-C [person performSelector:@selector(sayHello:inLanguage:) withObject:@"Lukas" withObject:@"French"];

Slide 39

Slide 39 text

SEL selector = NSSelectorFromString(@"sayHello:inLanguage:"); Objective-C !39

Slide 40

Slide 40 text

Objects

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

Foundation • The “standard library” of Objective-C

Slide 43

Slide 43 text

Ruby String

Slide 44

Slide 44 text

Objective-C NSString

Slide 45

Slide 45 text

Ruby Array

Slide 46

Slide 46 text

Objective-C NSArray

Slide 47

Slide 47 text

Ruby Hash

Slide 48

Slide 48 text

Objective-C NSDictionary

Slide 49

Slide 49 text

Objective-C NSMutableString NSMutableArray NSMutableDictionary

Slide 50

Slide 50 text

Ruby names = ['Mikael', 'Kristofer', 'Lukas'] ! name = names[1]

Slide 51

Slide 51 text

Objective-C NSArray *names = @[@"Mikael", @"Kristofer", @“Lukas”]; ! NSString *name = names[1];

Slide 52

Slide 52 text

Ruby locations = { 'Mikael' => 'Vienna', 'Kristofer' => 'Istanbul', 'Lukas' => 'Istanbul' } ! location = locations['Kristofer']

Slide 53

Slide 53 text

Objective-C NSDictionary *locations = @{ @"Mikael": @"Vienna", @"Kristofer": @"Istanbul", @"Lukas": @"Istanbul" }; ! NSString *location = locations[@"Kristofer"];

Slide 54

Slide 54 text

Why NS, KM, etc.?

Slide 55

Slide 55 text

• 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

Slide 56

Slide 56 text

Duck typing and introspection

Slide 57

Slide 57 text

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.

Slide 58

Slide 58 text

Ruby sender.respond_to?(:to_f)

Slide 59

Slide 59 text

Objective-C [sender respondsToSelector:@selector(floatValue)];

Slide 60

Slide 60 text

Ruby def action(sender) if sender.class == Array # ... elsif sender.class == Hash # ... end end

Slide 61

Slide 61 text

Objective-C - (void)action:(id)sender { if ([sender isKindOfClass:[UIButton class]]) { // ... } else if ([sender isKindOfClass:[UITapGestureRecognizer class]]) { // .. } }

Slide 62

Slide 62 text

Blocks

Slide 63

Slide 63 text

Block • Enumeration • Passing code around • Async tasks, completion handlers • Blocks are closures

Slide 64

Slide 64 text

Ruby block = lambda { puts 'hello, world' } ! block.call

Slide 65

Slide 65 text

Objective-C void (^block)() = ^ { NSLog(@"hello, world"); }; ! block();

Slide 66

Slide 66 text

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 }); });

Slide 67

Slide 67 text

How Do I Declare A Block in Objective-C? http://fuckingblocksyntax.com

Slide 68

Slide 68 text

Domain Specific Languages

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

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]; }); }); });

Slide 71

Slide 71 text

The Objective-C Runtime

Slide 72

Slide 72 text

The Objective-C Runtime #include

Slide 73

Slide 73 text

The Objective-C Runtime • objc_msgSend • class_getMethodImplementation • objc_allocateClassPair • objc_getClassList • etc.

Slide 74

Slide 74 text

Tools

Slide 75

Slide 75 text

rubygems and bundler # Gemfile ! source 'https://rubygems.org' ruby '2.1.0' ! gem 'rack' gem 'vienna', '>= 0.4.0' gem 'thin'

Slide 76

Slide 76 text

Cocoapods # Podfile ! platform :ios, '7.0' ! pod 'AFNetworking', '~> 2.0'

Slide 77

Slide 77 text

Where to Go from Here

Slide 78

Slide 78 text

Around the Web • iOS Developer Library • raywenderlich.com • Cocoapods

Slide 79

Slide 79 text

Books • Objective-C Programming: The Big Nerd Ranch Guide • iOS Programming: The Big Nerd Ranch Guide • iOS 7 Programming Fundamentals • Programming iOS 7

Slide 80

Slide 80 text

Thank You! • @mkonutgan on Twitter • @kmikael on GitHub • kmikael.com • [email protected]