My Way to Objective-C

F2dcf6633971844e19ca96ea294ba976?s=47 高見龍
November 22, 2012

My Way to Objective-C

F2dcf6633971844e19ca96ea294ba976?s=128

高見龍

November 22, 2012
Tweet

Transcript

  1. My Way to Obj-C

  2. None
  3. programming

  4. exp ≈ 15 years

  5. but not solid at all ..orz

  6. Flash/ActionScript

  7. exp ≈ 7 years

  8. Ruby

  9. exp ≈ 3.5 years

  10. Obj-C

  11. exp ≈ 2 years

  12. real exp ≈ 0.5 years

  13. I learn a lot from those languages.

  14. Obj-C

  15. Obj-C is a dynamic language.

  16. OO from smalltalk

  17. send message

  18. # Ruby class Animal def say_something(words) puts words end end

    cat = Animal.new cat.say_something "hello, world"
  19. // Obj-C [cat saySomething: @"Hello, World"];

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

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

  22. nil is fine with all messages which it doesn't know

    :)
  23. protocol

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

  25. // Animal.h @interface Animal<Talking> @end

  26. // Animal.m @implement Animal - (void)saySomething:(NSString *)word { // say

    something here.. :) } @end
  27. category

  28. monkey patching

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

    end "eddie".say_hi # hi, there
  30. // NSString+MyHelloString.h #import <Foundation/Foundation.h> @interface NSString (MyHelloString) - (void) say_hi;

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

    there"); } @end
  32. // Cat.h #import "NSString+MyHelloString.h"

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

  34. id

  35. dynamic typing

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

  37. - (id)init { self = [super init]; if (self) {

    // do some init things here } return self; }
  38. block

  39. ^

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

  41. # obj-c [kids enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

    // do something }];
  42. GCD

  43. # 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 }); });
  44. coding exp from Flash/ ActionScript and Ruby

  45. Obj-C and Ruby have the same parent

  46. smalltalk

  47. I learn dynamic programming from Ruby

  48. I learn block from Ruby

  49. Obj-C is not difficult to learn.

  50. Cocoa Framework is.

  51. I learn UI inheritance from Flash/ActionScript

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

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

  54. Essential ActionScript 3.0 p.459

  55. event handling

  56. async handling

  57. cocos2d

  58. But..

  59. delegation

  60. Obj-C is subset of C

  61. memory management

  62. retain v.s release

  63. -(NSString *) getBookName { NSString *the_name = @"This is a

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

    book"; [the_name release]; return the_name; }
  65. autorelease http://blog.eddie.com.tw/2010/11/25/autorelease-in-objective-c/

  66. -(NSString *) getBookName { NSString *the_name = @"This is a

    book"; return [the_name autorelease]; }
  67. None
  68. ARC Automatic Reference Counting

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

  70. new style Obj-C syntax

  71. literal syntax sugar

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

  73. NSNumber* age = @18;

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

  75. NSNumber* pi = @3.1415926F;

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

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

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

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

  80. // for-loop style NSArray* kids = ... for (int i

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

    in kids) { // do something }
  82. // block style NSArray* kids = ... [kids enumerateObjectsUsingBlock:^(NSString* kid,

    NSUInteger idx, BOOL *stop) { // do something here }];
  83. NSDictionary * kidsEnglishName = ... NSArray* keys = [dict allKeys];

    for (NSString* key in keys) { NSString* value = [dict objectForKey:key]; // do something else }
  84. // block style NSDictionary * kidsEnglishName = ... [kids enumerateKeysAndObjectsUsingBlock:^(id

    key, id obj, BOOL *stop) { // do something here }];
  85. Subscripting Methods

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

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

  88. NSMutableArray* kids = [@[@"高思", @"高齊"] mutableCopy]; kids[0] = @"狸貓"; NSLog(@"%@",

    [kids objectAtIndex:0]); // 狸貓
  89. NSMutableDictionary* kidsEnglishName = [@{@"高思":@"Kose", @"高齊":@"Coach"} mutableCopy]; kidsEnglishName[@"高思"] = @"Gauss";

  90. @synthesize

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

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

    name = _name;
  93. None
  94. Conclusion

  95. there's no BEST or WORST language

  96. non of them are useless

  97. code more, complain less

  98. 完 thank you all :)

  99. 高見見龍龍 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 eddie@digik.com.tw +886-928-617-687