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

詳解Objective-C

hirose yudai
September 17, 2017
3.2k

 詳解Objective-C

iOSDC 2017でお話しする内容です

https://iosdc.jp/2017/node/1364

Objective-Cについて熱く語ります

hirose yudai

September 17, 2017
Tweet

Transcript

  1. Ϋϥεͷએݴ CustomClass.h @interface CustomClass : NSObject // ͜͜ʹΠϯλʔϑΣʔε @end CustomClass.m

    #import “CustomClass.h" @implementation CustomClass // ͜͜ʹ࣮૷ @end I ϔομʔϑΝΠϧ ͱ N ϝΠϯϑΝΠϧ Λ࡞Δ 9DPEFͰΫϥεΛ࡞ΔͱͰ͖͍ͯ·͢
  2. Ϋϥεͷએݴ CustomClass.h @interface CustomClass : NSObject // ͜͜ʹΠϯλʔϑΣʔε @end CustomClass.m

    #import “CustomClass.h" @implementation CustomClass // ͜͜ʹ࣮૷ @end IϑΝΠϧ͸ΠϯλʔϑΣʔεΛఆٛ͢Δ ͨΊͷϑΝΠϧ IϑΝΠϧʹΫϥεΛએݴ͠ͳ͍ͱଞͷΫ ϥε͔Β͸ݟΔ͜ͱ͕Ͱ͖ͳ͍
  3. $MBTTͷએݴ CustomClass.h @interface CustomClass : NSObject // ͜͜ʹΠϯλʔϑΣʔε @end CustomClass.m

    #import “CustomClass.h" @implementation CustomClass // ͜͜ʹ࣮૷ @end ͍͍ͩͨ/40CKFDU΋͘͠͸ /40CKFDUΛܧঝ͍ͯ͠ΔΫϥεΛ ܧঝ͓͚ͯ͠͹0,
  4. Ϋϥεͷએݴ CustomClass.h @interface CustomClass : NSObject // ͜͜ʹΠϯλʔϑΣʔε @end CustomClass.m

    #import “CustomClass.h" @implementation CustomClass // ͜͜ʹ࣮૷ @end NʹIͰએݴͨ͠಺༰Λ࣮૷͍ͯ͘͠
  5. CustomClass.h @interface CustomClass : NSObject + (instancetype)method; - (void)method:(NSObject *)hoge;

    - (NSObject *)method:(NSObject *)hoge with:(NSObject *)fuga; @end ͜Μͳײ͡
  6. CustomClass.h @interface CustomClass : NSObject + (instancetype)method; - (void)method:(NSObject *)hoge;

    - (NSObject *)method:(NSObject *)hoge with:(NSObject *)fuga; @end ϝιουͷએݴ ͕͜͜ฦΓ஋
  7. CustomClass.h @interface CustomClass : NSObject + (instancetype)method; - (void)method:(NSObject *)hoge;

    - (NSObject *)method:(NSObject *)hoge with:(NSObject *)fuga; @end ϝιουͷએݴ ϝιουͷ໊લ
  8. CustomClass.h @interface CustomClass : NSObject + (instancetype)method; - (void)method:(NSObject *)hoge;

    - (NSObject *)method:(NSObject *)hoge with:(NSObject *)fuga; @end ϝιουͷએݴ ୈೋҾ਺Ҏ߱ʹ͸-BCFM͕ͭ͘
  9. CustomClass.h @interface CustomClass : NSObject + (instancetype)method; - (void)method:(NSObject *)hoge;

    - (NSObject *)method:(NSObject *)hoge with:(NSObject *)fuga; @end ϝιουͷએݴ A A͕Ϋϥεϝιου 4XJGUͩͱADMBTTGVODA AA͕Πϯελϯεϝιου 4XJGUͩͱAGVODA
  10. CustomClass.h @interface CustomClass : NSObject + (instancetype)method; - (void)method:(NSObject *)hoge;

    - (NSObject *)method:(NSObject *)hoge with:(NSObject *)fuga; @end ϝιουͷએݴ AJOTUBODFUZQFA͸ࣗ෼ͷ
 ΠϯελϯεͷܕΛද͢એݴ
 ܧঝઌͰ͜ͷϝιουΛ࢖ͭ৔߹
 ܧঝઌͷܕͱͯ͠ฦΔ
  11. @interface CustomClass : NSObject + (instancetype)method; @end @interface CustomClass2 :

    CustomClass //- (instancetype)method; @end JOTUBODFUZQFʹ͍ͭͯ
  12. @interface CustomClass : NSObject + (instancetype)method; @end @interface CustomClass2 :

    CustomClass //- (instancetype)method; @end JOTUBODFUZQF ͜ͷ৔߹ͷAJOTUBODFUZQFA͸ $VTUPN$MBTT
  13. @interface CustomClass : NSObject + (instancetype)method; @end @interface CustomClass2 :

    CustomClass //- (instancetype)method; @end JOTUBODFUZQF ͜ͷ৔߹ͷAJOTUBODFUZQFA͸ $VTUPN$MBTT
  14. CustomClass.m
 #import “CustomClass.h" @implementation CustomClass + (instancetype)method1 { CustomClass *custom

    = [[CustomClass alloc] init]; return custom; } - (void)method: (NSObject *)hoge { // do something... } @end ͜Μͳײ͡
  15. CustomClass.m
 #import “CustomClass.h" @implementation CustomClass + (instancetype)method1 { CustomClass *custom

    = [[CustomClass alloc] init]; return custom; } - (void)method: (NSObject *)hoge { // do something... } @end ϝιουͷ࣮૷ IͰॻ͔Ε͍ͯΔ௨Γʹએݴ͠ɺ
 \^είʔϓͷதͰϝιουͷ࣮૷
  16. CustomClass.m
 #import “CustomClass.h" @implementation CustomClass + (instancetype)method1 { CustomClass *custom

    = [[CustomClass alloc] init]; return custom; } - (void)method: (NSObject *)hoge { // do something... } @end ϝιουͷ࣮૷ ϩʔΧϧม਺΍ASFUVSOAจͱ͔ࡉ͔͍͜ ͱʹ͍ͭͯ͸ׂѪ
  17. @interface CustomClass : NSObject + (instancetype)method; @end @implementation CustomClass2 -

    (void)someMethod { CustomClass* object = [[CustomClass alloc] init]; [object method]; } @end ϝιουͷݺͼग़͠
  18. @interface CustomClass : NSObject + (instancetype)method; @end @implementation CustomClass2 -

    (void)someMethod { CustomClass* object = [[CustomClass alloc] init]; [object method]; } @end $VTUPN$MBTT͔Β$VTUPN$MBTTΛ࢖͏ ҧ͏Ϋϥε
  19. @interface CustomClass : NSObject + (instancetype)method; @end @implementation CustomClass2 -

    (void)someMethod { CustomClass* object = [[CustomClass alloc] init]; [object method]; } @end ϝιουͷݺͼग़͠ ͜ͷೋͭʹ஫໨
  20. @interface CustomClass : NSObject - (instancetype)method; @end @implementation CustomClass2 -

    (void)someMethod { CustomClass* object = [[CustomClass alloc] init]; [object method]; } @end ϝιουͷݺͼग़͠ 0CKFDUJWF$ͷϝιουͷݺͼग़͠͸
 <A3FDFJWFSANFUIPE> ͷܗʹͳΔ
  21. @interface CustomClass : NSObject - (instancetype)method; @end @implementation CustomClass2 -

    (void)someMethod { CustomClass* object = [[CustomClass alloc] init]; [object method]; } @end ϝιουͷݺͼग़͠ ͜ͷ৔߹͸
 A3FDFJWFSAA$VTUPN$MBTTA ΫϥεʹϝιουΛૹ͍ͬͯΔͷͰ
 ͜Ε͸Ϋϥεϝιουͷݺͼग़͠
  22. @interface CustomClass : NSObject - (instancetype)method; @end @implementation CustomClass2 -

    (void)someMethod { CustomClass* object = [[CustomClass alloc] init]; [object method]; } @end ϝιουͷݺͼग़͠ ͜ͷ৔߹͸
 A3FDFJWFSAAPCKFDUA $VTUPN$MBTTܕͷΠϯελϯεʹ
 ϝιουΛૹ͍ͬͯΔͷͰ Πϯελϯεϝιου
  23. @interface CustomClass : NSObject - (instancetype)method; @end @implementation CustomClass2 -

    (void)someMethod { CustomClass* object = [[CustomClass alloc] init]; [[object method] method]; } @end ϝιουͷݺͼग़͠ ͪͳΈʹϝιουνΣΠϯΛ
 ͢Δͱ͜͏ͳΔ
  24. @interface CustomClass : NSObject + (instancetype)method; @end @implementation CustomClass2 -

    (void)someMethod { CustomClass* object = [[CustomClass alloc] init]; [[[[object method] method] method]; } @end ϝιουͷݺͼग़͠ ʜ
  25. ʜ

  26. [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ [[[[[[[[[[[[object method] method] method] method] method] method] method] method]

    method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] method] ; ϝιουνΣΠϯΛ͢Δͱ<>͕͍ͬͺ͍ʹ ͳΔ
  27. #MPDLTͷϓϩύςΟΛ࢖͏ ࢖͏ - (void)someMethod { self.blocks = ^NSObject* (NSInteger integer)

    { NSLog(@"log: %ld", integer); return [[NSObject alloc] init]; }; self.blocks(1); // log: 1 }
  28. #MPDLTͷϓϩύςΟΛ࢖͏ ࢖͏ - (void)someMethod { self.blocks = ^NSObject* (NSInteger integer)

    { NSLog(@"log: %ld", integer); return [[NSObject alloc] init]; }; self.blocks(1); // log: 1 } ฦΓ஋
  29. #MPDLTͷϓϩύςΟΛ࢖͏ ࢖͏ - (void)someMethod { self.blocks = ^NSObject* (NSInteger integer)

    { NSLog(@"log: %ld", integer); return [[NSObject alloc] init]; }; self.blocks(1); // log: 1 } Ҿ਺
  30. #MPDLTͷϓϩύςΟΛ࢖͏ ࢖͏ - (void)someMethod { self.blocks = ^NSObject* (NSInteger integer)

    { NSLog(@"log: %ld", integer); return [[NSObject alloc] init]; }; self.blocks(1); // log: 1 } \^ͷத਎͕࣮૷
  31. #MPDLTͷϓϩύςΟΛ࢖͏ ࢖͏ - (void)someMethod { self.blocks = ^NSObject* (NSInteger integer)

    { NSLog(@"log: %ld", integer); return [[NSObject alloc] init]; }; self.blocks(1); // log: 1 } ࣮ߦ͢Δͱ͖
  32. 4FMGΛΩϟϓνϟ͍ͨ͠ͱ͖ ࢖͏ - (void)someMethod { self.blocks = ^NSObject* (NSInteger integer)

    { NSLog(@"log: %@", self); return [[NSObject alloc] init]; }; } TFMGΛΩϟϓνϟ͢Δͱ
 ॥؀ࢀর͕ى͖ͯ͠·͏
  33. ऑࢀরΛ࢖͏ ࢖͏ - (void)someMethod { __weak CustomClass *weakSelf = self;

    self.blocks = ^NSObject* (NSInteger integer) { NSLog(@"log: %@", weakSelf); return [[NSObject alloc] init]; }; }
  34. ऑࢀরΛ࢖͏ ࢖͏ - (void)someMethod { __weak CustomClass *weakSelf = self;

    self.blocks = ^NSObject* (NSInteger integer) { NSLog(@"log: %@", weakSelf); return [[NSObject alloc] init]; }; } XFBLͷTFMGͷࢀরΛ࣋ͭ
  35. ऑࢀরΛ࢖͏ ࢖͏ - (void)someMethod { __weak CustomClass *weakSelf = self;

    self.blocks = ^NSObject* (NSInteger integer) { NSLog(@"log: %@", weakSelf); return [[NSObject alloc] init]; }; } CMPDLTͷதͰ͸XFBL4FMGΛ࢖͏
  36. ຊ୊ جຊ Ϋϥεͷએݴ ϝιου એݴ ࣮૷ ݺͼग़͠ #MPDLT .BDSP Ԡ༻

    - Type inference. - Method chain. - The strongest Objective-C I thought ͔͜͜Β͕ຊ౰ʹ࿩͔ͨͬͨ͠಺༰ͩ͊ʂ
  37. @@BVUP@UZQF - (void)someMethod { __auto_type string = @"hogehoge"; string.length; }

    /44USJOHͷMFOHUI
 ϓϩύςΟʹΞΫηεͰ͖Δ ͭ·Γܕਪ࿦Λͯ͘͠ΕΔ
  38. ๨Εͨਓ͸ޙͰεϥΠυݟ௚ͯ͠Ͷ @interface CustomClass : NSObject @property (nonatomic, copy) CustomClass* (^method)();

    @property (nonatomic, copy) CustomClass* (^chain)(); @end ฦΓ஋͖ͭCMPDLTͷϓϩύςΟΛએݴ͢Δ
  39. #define let __auto_type const #define var __auto_type #define dump(count) NSLog(@“count:

    %ld”, count) #define print() NSLog(@"ίίϮτΦολϤʔ") NSArray* Array() { return @[]; } - (instancetype)init { self = [super init]; if (self) { self.method = ^{ print(); }; } return self; } - (void)someMethod { let array = Array(); var array2 = Array(); array2 = @[@"hoge", @"fuga"]; // array = @[@"hoge", @"fuga"]; ίϯύΠϧΤϥʔ self.method(); dump(array.count); dump(array2.count); }
  40. #define let __auto_type const #define var __auto_type #define dump(count) NSLog(@“count:

    %ld”, count) #define print() NSLog(@"ίίϮτΦολϤʔ") NSArray* Array() { return @[]; } - (instancetype)init { self = [super init]; if (self) { self.method = ^{ print(); }; } return self; } - (void)someMethod { let array = Array(); var array2 = Array(); array2 = @[@"hoge", @"fuga"]; // array = @[@"hoge", @"fuga"]; ίϯύΠϧΤϥʔ self.method(); dump(array.count); dump(array2.count); } ๻ͷ͔Μ͕͑ͨ࠷ڧͷ0CKFDUJWF$
  41. #define let __auto_type const #define var __auto_type #define dump(count) NSLog(@“count:

    %ld”, count) #define print() NSLog(@"ίίϮτΦολϤʔ") NSArray* Array() { return @[]; } - (instancetype)init { self = [super init]; if (self) { self.method = ^{ print(); }; } return self; } - (void)someMethod { let array = Array(); var array2 = Array(); array2 = @[@"hoge", @"fuga"]; // array = @[@"hoge", @"fuga"]; ίϯύΠϧΤϥʔ self.method(); dump(array.count); dump(array2.count); } ࣮ߦ݁Ռ ίίϮτΦολϤʔ count: 0 count: 2
  42. #define let __auto_type const #define var __auto_type #define dump(count) NSLog(@“count:

    %ld”, count) #define print() NSLog(@"ίίϮτΦολϤʔ") NSArray* Array() { return @[]; } - (instancetype)init { self = [super init]; if (self) { self.method = ^{ print(); }; } return self; } - (void)someMethod { let array = Array(); var array2 = Array(); array2 = @[@"hoge", @"fuga"]; // array = @[@"hoge", @"fuga"]; ίϯύΠϧΤϥʔ self.method(); dump(array.count); dump(array2.count); } ܕਪ࿦͕Ͱ͖Δʂ
 MFUͰఆ਺Λ؆୯ʹએݴʂ
  43. #define let __auto_type const #define var __auto_type #define dump(count) NSLog(@“count:

    %ld", count) #define print() NSLog(@"ίίϮτΦολϤʔ") NSArray* Array() { return @[]; } - (instancetype)init { self = [super init]; if (self) { self.method = ^{ print(); }; } return self; } - (void)someMethod { let array = Array(); var array2 = Array(); array2 = @[@"hoge", @"fuga"]; // array = @[@"hoge", @"fuga"]; ίϯύΠϧΤϥʔ self.method(); dump(array.count); dump(array2.count); } ΋ͬͱ؆୯ʹϩάΛు͘Α͏ʹʂ
 ϚΫϩ͸෼ذॲཧ΋͔͚ΔͷͰ
 ඞཁ͕͋Ε͹֦ு
  44. #define let __auto_type const #define var __auto_type #define dump(count) NSLog(@“count:

    %ld”, count) #define print() NSLog(@"ίίϮτΦολϤʔ") NSArray* Array() { return @[]; } - (instancetype)init { self = [super init]; if (self) { self.method = ^{ print(); }; } return self; } - (void)someMethod { let array = Array(); var array2 = Array(); array2 = @[@"hoge", @"fuga"]; // array = @[@"hoge", @"fuga"]; ίϯύΠϧΤϥʔ self.method(); dump(array.count); dump(array2.count); } $ݴޠͷTZOUBYΛ࢖ͬͯ ؾܰʹJOJUJBMJ[FSΛ࢖͑ΔΑ͏ʹʂ ͦͯ͠ɺ/4ϓϨϑΟοΫε΋ͱΔʂ