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

Objective-C Awesomesauce!

Objective-C Awesomesauce!

Ruby and Objective-C seem like programming languages from two different worlds. When you take a closer look at them, you will start to see similarities. As a Ruby developer, I want to highlight these similarities in this talks and learn from what Objective-C does differently from Ruby.

Ariejan de Vroom

April 03, 2014
Tweet

More Decks by Ariejan de Vroom

Other Decks in Programming

Transcript

  1. // ADVPerson.h ! #import <Foundation/Foundation.h> #import "ADVDepartment.h" ! @interface ADVPerson

    : NSObject { } ! @property (copy, nonatomic) NSString *name; @property (strong, nonatomic) ADVDepartment *department; @property (readonly, nonatomic) NSNumber *salary; ! + (ADVPerson *)personWithRandomData; ! - (void)giveRaiseOf:(NSNumber)amount withReason:(NSString *)reason; ! @end
  2. // ADVPerson.h ! #import <Foundation/Foundation.h> #import "ADVDepartment.h" ! @interface ADVPerson

    : NSObject { } ! @property (copy, nonatomic) NSString *name; @property (strong, nonatomic) ADVDepartment *department; @property (readonly, nonatomic) NSNumber *salary; ! + (ADVPerson *)personWithRandomData; ! - (void)giveRaiseOf:(NSNumber)amount withReason:(NSString *)reason; ! @end
  3. // ADVPerson.h ! #import <Foundation/Foundation.h> #import "ADVDepartment.h" ! @interface ADVPerson

    : NSObject { } ! @property (copy, nonatomic) NSString *name; @property (strong, nonatomic) ADVDepartment *department; @property (readonly, nonatomic) NSNumber *salary; ! + (ADVPerson *)personWithRandomData; ! - (void)giveRaiseOf:(NSNumber)amount withReason:(NSString *)reason; ! @end
  4. // ADVPerson.h ! #import <Foundation/Foundation.h> #import "ADVDepartment.h" ! @interface ADVPerson

    : NSObject { } ! @property (copy, nonatomic) NSString *name; @property (strong, nonatomic) ADVDepartment *department; @property (readonly, nonatomic) NSNumber *salary; ! + (ADVPerson *)personWithRandomData; ! - (void)giveRaiseOf:(NSNumber)amount withReason:(NSString *)reason; ! @end
  5. // ADVPerson.m ! #import "ADVPerson.h" ! @interface ADVPerson () @end

    ! @implementation ADVPerson ! + (ADVPerson *)personWithRandomData { ADVPerson *person = [[ADVPerson alloc] init]; [person setName:@"John Doe"]; [person setDepartment:[ADVDepartment randomDepartment]]; [person giveRaiseOf:2000 withReason:@"Initial salary"]; return person; } ! - (void)giveRaiseOf:(NSNumber)amount withReason:(NSString *)reason { _salary = _salary + amount; NSLog(@"Raising salary to %@ because %@", _salary, reason); } ! @end
  6. // ADVPerson.m ! #import "ADVPerson.h" ! @interface ADVPerson () @end

    ! @implementation ADVPerson ! + (ADVPerson *)personWithRandomData { ADVPerson *person = [[ADVPerson alloc] init]; [person setName:@"John Doe"]; [person setDepartment:[ADVDepartment randomDepartment]]; [person giveRaiseOf:2000 withReason:@"Initial salary"]; return person; } ! - (void)giveRaiseOf:(NSNumber)amount withReason:(NSString *)reason { _salary = _salary + amount; NSLog(@"Raising salary to %@ because %@", _salary, reason); } ! @end
  7. // ADVPerson.m ! #import "ADVPerson.h" ! @interface ADVPerson () -

    (void)setSalary:(NSNumber)amount; @end ! @implementation ADVPerson ! + (ADVPerson *)personWithRandomData { ADVPerson *person = [[ADVPerson alloc] init]; [person setName:@"John Doe"]; [person setDepartment:[ADVDepartment randomDepartment]]; [person giveRaiseOf:2000 withReason:@"Initial salary"]; return person; } ! - (void)giveRaiseOf:(NSNumber)amount withReason:(NSString *)reason { [self setSalary:(_salary + amount)]; NSLog(@"Raising salary to %@ because %@", _salary, reason); } ! @end
  8. // NSString+Reverse.h @interface NSString (Reverse) { } - (NSString *)reverse;

    @end ! ! ! ! // NSString+Reverse.m @implementation NSString (Reverse) ! - (NSString *)reverse { // TODO: Implement this. } ! @end
  9. Memory <ADVPerson name: “John Doe”> ADVPerson *john = [ADVPerson personWithRandomData];

    ! NSLog(@"Person: %@", john.name); // Person: John Doe The Stack john
  10. Memory <ADVPerson name: “John Doe”> ADVPerson *john = [ADVPerson personWithRandomData];

    ! NSLog(@"Person: %@", john.name); // Person: John Doe ! ADVPerson *dick = john; The Stack john dick
  11. Memory <ADVPerson name: “Dick Doe”> ADVPerson *john = [ADVPerson personWithRandomData];

    ! NSLog(@"Person: %@", john.name); // Person: John Doe ! ADVPerson *dick = john; [dick setName:@"Dick Doe”]; The Stack john dick
  12. Memory <ADVPerson name: “Dick Doe”> ADVPerson *john = [ADVPerson personWithRandomData];

    ! NSLog(@"Person: %@", john.name); // Person: John Doe ! ADVPerson *dick = john; [dick setName:@"Dick Doe”]; ! NSLog(@"Person: %@", dick.name); // Person: Dick Doe The Stack john dick
  13. Memory <ADVPerson name: “Dick Doe”> ADVPerson *john = [ADVPerson personWithRandomData];

    ! NSLog(@"Person: %@", john.name); // Person: John Doe ! ADVPerson *dick = john; [dick setName:@"Dick Doe”]; ! NSLog(@"Person: %@", dick.name); // Person: Dick Doe ! NSLog(@"Person: %@", john.name); // Person: Dick Doe The Stack john dick
  14. NSString *s = [[NSString alloc] init]; // Ref count is

    1 [s retain]; // Ref count is 2 [s release]; // Ref count is 1 again
  15. NSString *s = [[NSString alloc] init]; // Ref count is

    1 [s retain]; // Ref count is 2 [s release]; // Ref count is 1 again [s release]; // Ref count is 0
  16. NSString *s = [[NSString alloc] init]; // Ref count is

    1 [s retain]; // Ref count is 2 [s release]; // Ref count is 1 again [s release]; // Ref count is 0 s = nil; // Remove our pointer
  17. NSString *s = [[NSString alloc] init]; // Ref count is

    1 [s retain]; // Ref count is 2 [s release]; // Ref count is 1 again [s release]; // Ref count is 0 s = nil; // Remove our pointer ! ! NSString *s = [NSString stringWithString:@"Hello world"]; // Auto released! [s retain]; // To hang on to s
  18. NSString *s = [[NSString alloc] init]; // Ref count is

    1 [s retain]; // Ref count is 2 [s release]; // Ref count is 1 again [s release]; // Ref count is 0 s = nil; // Remove our pointer ! ! NSString *s = [NSString stringWithString:@"Hello world"]; // Auto released! [s retain]; // To hang on to s ! ! - (NSString *)helloWorldString { NSString *s = [[NSString alloc] initWithString:@"Hello world!"]; return [s autorelease]; }
  19. // ADVPriceFetcher.h #include <Foundation/Foundation.h> ! @protocol ADVPriceFetcherDelegate - (void)fetchedCurrentBitcoinPriceInEur:(NSNumber *)price;

    @end ! @interface ADVPriceFetcher : NSObject @property (weak, nonatomic) id<ADVPriceFetcherDelegate> delegate; - (void)fetchCurrentBitcoinPriceInEur; @end
  20. // ADVMainViewController.h #import "ADVPriceFetcher.h" ! @interface ADVMainViewController : UIViewController <ADVPriceFetcherDelegate>

    @property (strong, nonatomic) ADVPriceFetcher *priceFetcher; - (IBAction)pushRefreshButton; @end
  21. // ADVMainViewController.m @implementation ADVMainViewController ! - (id)init { if ([super

    init]) { self.priceFetcher = [[ADVPriceFetcher alloc] init]; [self.priceFetcher setDelegate:self]; } return self; } ! - (IBAction)pushRefreshButton { [self.priceFetcher fetchCurrentBitcoinPriceInEur]; } ! - (void)fetchedCurrentBitcoinPriceInEur:(NSNumber *)price { NSLog(@"Fetch price: %@", price); } ! @end
  22. // ADVPriceFetcher.m #import "ADVPriceFetch.h" ! @implementation ADVPriceFetcher ! - (void)fetchCurrentBitcoinPriceInEur

    { // Work our networking magic. NSNumber *price = /* ... */ if (_delegate && [_delegate respondsToSelector:@selector(fetchedCurrentBitcoinPriceInEur:)]) { [delegate fetchedCurrentBitcoinPriceInEur:price]; } } ! @end
  23. // ADVPriceFetcher.h #include <Foundation/Foundation.h> @interface ADVPriceFetcher : NSObject - (void)fetchCurrentBitcoinPriceInEur:(void

    (^)(NSNumber *price)completed; @end ! ! // ADVPriceFetcher.m @implementation ADVPriceFetcher ! - (void)fetchCurrentBitcoinPriceInEur:(void (^)(NSNumber *price)completed { // Work our networking magic. NSNumber *price = /* ... */ completed(price); } @end