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

My Way to Objective-C

高見龍
November 22, 2012

My Way to Objective-C

高見龍

November 22, 2012
Tweet

More Decks by 高見龍

Other Decks in Programming

Transcript

  1. My Way to Obj-C

    View full-size slide

  2. exp ≈ 15 years

    View full-size slide

  3. but not solid at all ..orz

    View full-size slide

  4. Flash/ActionScript

    View full-size slide

  5. exp ≈ 7 years

    View full-size slide

  6. exp ≈ 3.5 years

    View full-size slide

  7. exp ≈ 2 years

    View full-size slide

  8. real exp ≈ 0.5 years

    View full-size slide

  9. I learn a lot from those
    languages.

    View full-size slide

  10. Obj-C is a dynamic
    language.

    View full-size slide

  11. OO from smalltalk

    View full-size slide

  12. send message

    View full-size slide

  13. # Ruby
    class Animal
    def say_something(words)
    puts words
    end
    end
    cat = Animal.new
    cat.say_something "hello, world"

    View full-size slide

  14. // Obj-C
    [cat saySomething: @"Hello, World"];

    View full-size slide

  15. # Ruby
    cat.send :say_something, "Hello, World"

    View full-size slide

  16. but what if the cat
    doesn't know how to
    talk?

    View full-size slide

  17. nil is fine with all
    messages which it
    doesn't know :)

    View full-size slide

  18. @protocol Talking
    - (void)saySomething:(NSString *)word;
    @end

    View full-size slide

  19. // Animal.h
    @interface Animal
    @end

    View full-size slide

  20. // Animal.m
    @implement Animal
    - (void)saySomething:(NSString *)word {
    // say something here.. :)
    }
    @end

    View full-size slide

  21. monkey patching

    View full-size slide

  22. # Ruby
    class String
    def say_hi
    puts "hi, there"
    end
    end
    "eddie".say_hi # hi, there

    View full-size slide

  23. // NSString+MyHelloString.h
    #import
    @interface NSString (MyHelloString)
    - (void) say_hi;
    @end

    View full-size slide

  24. // NSString+MyHelloString.m
    #import "NSString+MyHelloString.h"
    @implementation NSString (MyHelloString)
    -(void) say_hi{
    NSLog(@"hi, there");
    }
    @end

    View full-size slide

  25. // Cat.h
    #import "NSString+MyHelloString.h"

    View full-size slide

  26. // Cat.m
    [@"eddie" say_hi];

    View full-size slide

  27. dynamic typing

    View full-size slide

  28. // Cat.h
    @interface Cat
    - (IBAction)clickButton:(id)sender;
    @end

    View full-size slide

  29. - (id)init
    {
    self = [super init];
    if (self) {
    // do some init things here
    }
    return self;
    }

    View full-size slide

  30. # Ruby
    10.times { |i|
    puts i
    }

    View full-size slide

  31. # obj-c
    [kids enumerateObjectsUsingBlock:^(id obj,
    NSUInteger idx, BOOL *stop) {
    // do something
    }];

    View full-size slide

  32. # obj-c
    dispatch_queue_t queue =
    dispatch_queue_create("imageLoadingQueue", NULL);
    dispatch_async(queue, ^{
    // start to load image here
    dispatch_async(dispatch_get_main_queue(), ^{
    // image loaded, do something here
    });
    });

    View full-size slide

  33. coding exp from Flash/
    ActionScript and Ruby

    View full-size slide

  34. Obj-C and Ruby have the
    same parent

    View full-size slide

  35. I learn dynamic
    programming from Ruby

    View full-size slide

  36. I learn block from Ruby

    View full-size slide

  37. Obj-C is not difficult to
    learn.

    View full-size slide

  38. Cocoa Framework is.

    View full-size slide

  39. I learn UI inheritance
    from Flash/ActionScript

    View full-size slide

  40. https://developer.apple.com/Library/ios/#docum
    Cocoa/Conceptual/CocoaFundamental
    CocoaDesignPatterns/CocoaDesignPattern

    View full-size slide

  41. https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/WhatIsCocoa/WhatIsCocoa.html

    View full-size slide

  42. Essential ActionScript 3.0 p.459

    View full-size slide

  43. event handling

    View full-size slide

  44. async handling

    View full-size slide

  45. Obj-C is subset of C

    View full-size slide

  46. memory management

    View full-size slide

  47. retain v.s release

    View full-size slide

  48. -(NSString *) getBookName
    {
    NSString *the_name = @"This is a book";
    return the_name;
    }

    View full-size slide

  49. -(NSString *) getBookName
    {
    NSString *the_name = @"This is a book";
    [the_name release];
    return the_name;
    }

    View full-size slide

  50. autorelease
    http://blog.eddie.com.tw/2010/11/25/autorelease-in-objective-c/

    View full-size slide

  51. -(NSString *) getBookName
    {
    NSString *the_name = @"This is a book";
    return [the_name autorelease];
    }

    View full-size slide

  52. ARC
    Automatic Reference Counting

    View full-size slide

  53. ARC knows more Obj-C
    than you :)

    View full-size slide

  54. new style Obj-C syntax

    View full-size slide

  55. literal syntax sugar

    View full-size slide

  56. NSNumber* age = [NSNumber numberWithInt:18];

    View full-size slide

  57. NSNumber* age = @18;

    View full-size slide

  58. NSNumber* pi =
    [NSNumber numberWithFloat:3.1415926F];

    View full-size slide

  59. NSNumber* pi = @3.1415926F;

    View full-size slide

  60. NSArray* kids =
    [NSArray arrayWithObjects:@"高思", @"高齊", nil];

    View full-size slide

  61. NSArray* kids = @[@"高思", @"高齊"];

    View full-size slide

  62. NSDictionary* kidsEnglishName =
    [NSDictionary dictionaryWithObjectsAndKeys:
    @"Kose", @"高思",
    @"Coach", @"高齊",
    nil];

    View full-size slide

  63. NSDictionary* kidsEnglishName =
    @{@"高思":@"Kose", @"高齊":@"Coach"};

    View full-size slide

  64. // for-loop style
    NSArray* kids = ...
    for (int i = 0; i < kids.count; ++i) {
    NSString* kid = [kids objectAtIndex:i];
    // do something
    }

    View full-size slide

  65. // for-in style
    NSArray* kids = ...
    for (NSString* kid in kids) {
    // do something
    }

    View full-size slide

  66. // block style
    NSArray* kids = ...
    [kids enumerateObjectsUsingBlock:^(NSString* kid,
    NSUInteger idx, BOOL *stop) {
    // do something here
    }];

    View full-size slide

  67. NSDictionary * kidsEnglishName = ...
    NSArray* keys = [dict allKeys];
    for (NSString* key in keys) {
    NSString* value = [dict objectForKey:key];
    // do something else
    }

    View full-size slide

  68. // block style
    NSDictionary * kidsEnglishName = ...
    [kids enumerateKeysAndObjectsUsingBlock:^(id key,
    id obj, BOOL *stop) {
    // do something here
    }];

    View full-size slide

  69. Subscripting Methods

    View full-size slide

  70. NSArray* kids = @[@"高思", @"高齊"];
    NSLog(@"%@", [kids objectAtIndex:0]); // 高思

    View full-size slide

  71. NSArray* kids = @[@"高思", @"高齊"];
    NSLog(@"%@", kids[0]); // 高思

    View full-size slide

  72. NSMutableArray* kids =
    [@[@"高思", @"高齊"] mutableCopy];
    kids[0] = @"狸貓";
    NSLog(@"%@", [kids objectAtIndex:0]); // 狸貓

    View full-size slide

  73. NSMutableDictionary* kidsEnglishName =
    [@{@"高思":@"Kose",
    @"高齊":@"Coach"} mutableCopy];
    kidsEnglishName[@"高思"] = @"Gauss";

    View full-size slide

  74. // hello.h
    @property (nonatomic, strong) NSString* name;
    // hello.m
    @synthesize name = _name;

    View full-size slide

  75. // hello.h
    @property (nonatomic, strong) NSString* name;
    // hello.m
    @synthesize name = _name;

    View full-size slide

  76. there's no BEST or
    WORST language

    View full-size slide

  77. non of them are useless

    View full-size slide

  78. code more,
    complain less

    View full-size slide


  79. thank you all :)

    View full-size slide

  80. 高見見龍龍
    Conacts
    photo by Eddie
    Websie
    Blog
    Plurk
    Facebook
    Google Plus
    Twiter
    Email
    Mobile
    http://www.eddie.com.tw
    http://blog.eddie.com.tw
    http://www.plurk.com/aquarianboy
    http://www.facebook.com/eddiekao
    http://www.eddie.com.tw/+
    https://twiter.com/#!/eddiekao
    [email protected]
    +886-928-617-687

    View full-size slide