Slide 1

Slide 1 text

OBJECTIVE-C 2014/03/15 SAT. NAGAOKA DEVELOPERS STUDY #36 DICTAV (SHINTARO ABE)

Slide 2

Slide 2 text

DICTAV • ৽ׁࢢࡏॅ IOSϓϩ άϥϚ

Slide 3

Slide 3 text

ϓϩάϥϛϯάݴޠྺ ߴઐॳظ HTML/C/Java/BASIC ߴઐதظ ߴઐ࣌୅ɺ ߴઐޙظ ߴઐͷଔ࿦Ͱແཧ໼ཧ ߴઐޙظ ߴઐͷޙഐʹ ࣗӴୂॳظ MFC ࣗӴୂதظ JOVIAL ࣗӴୂޙظ Ruby ϑϦʔ Objective-C ࠷ۙ Go

Slide 4

Slide 4 text

OBJECTIVE-C #import ! @interface MyObject : NSObject @end ! @implementation MyObject - (void) helloWorld { printf("Hello, World!\n"); } @end ! ! int main(int argc, char **argv) { id obj = [MyObject new]; [obj helloWorld]; ! return 0; }

Slide 5

Slide 5 text

OBJECTIVE-C ͱ͸ • 1983೥ੜ·ΕʢC++ͱಉ͡ʣ • C ͷεʔύʔηοτ • C++: better C • Objective-C: C & Object System • runtime + Foundation framework (+ AppKit)

Slide 6

Slide 6 text

MACઐ༻Ͱ͠ΐʁ • GNUStep ΛೖΕΕ͹WindowsͰ΋͏͘͝ • Grand Central Dispatch • Blocks • Clang + LLVM

Slide 7

Slide 7 text

GNUSTEP • ϓϩϓϥΠΤλϦͳιϑτ΢ΣΞͰ͋ͬͨ NEXTSTEP ΛϋοΫͨ͠ͷ͕࢝·Γ • NeXT(+Sun Microsystems)͕OPENSTEPͱͯ͠ެ։ • OPENSTEP࢓༷ʹ׬શޓ׵ͳϓϥοτϑΥʔϜ • Cocoaʹ௥ਵ

Slide 8

Slide 8 text

ͲΜͳݴޠ • ΦϒδΣΫτࢦ޲ݴޠ • ΦϒδΣΫτͷϝιουݺͼग़͠͸[]Ͱғ·Εͨ ϝοηʔδࣜ

Slide 9

Slide 9 text

Ϋϥε @interface MyObject : NSObject @property (strong) NSString *username; - (id)initWithName:(NSString*)name; - (void)helloToName:(NSString*)name; @end ! @implementation MyObject { NSString *myName; } - (id)initWithName:(NSString*)name { self = [super init]; myName = name return self; } - (void)helloToName:(NSString*)name { printf(“hello %s\n”, [name UTF8String]); } @end

Slide 10

Slide 10 text

Α͘࢖͏Ϋϥε • id (Any) • NSString • NSNumber • NSArray • NSDictionary • NSURL • NSURLConnection (NSURLSession) • NSOperationQueue

Slide 11

Slide 11 text

೔ຊਓ͕஌͓ͬͯ͘΂͖ ৽̏େɾOBJECTIVE-CϓϩάϥϛϯάελΠϧ • σϦήʔτ • Φϒαʔόʔ • ࢀরΧ΢ϯτํࣜ • ARC : Automatic Reference Counting

Slide 12

Slide 12 text

σϦήʔτ • GoFຊͷAdapterύλʔϯ • UIपΓͰ޿͘׆༻͞Ε͍ͯΔ

Slide 13

Slide 13 text

σϦήʔτ @protocol DLDelegate - (NSURL*)nextURL; - (void)didFinishDownload:(id)object; @end ! @interface MyDownloader : NSObject @property (assign) id delegate @end ! @implementation MyDownloader - (void) downloadAsync { NSURL *url; while(url = [self.delegate nextURL]){ /* μ΢ϯϩʔυ*/ [self.delegate didFinishDownload:obj]; } } @end @interface MyObj:NSObject @end ! @implementation MyObject { MyDownloader *dw; } - (id)init { self = [super init]; dw = [MyDownloader new]; dw.delegate = self; [dw downloadAsync]; return self; } - (NSURL*)nextURL { NSURL *u = //… return u; } - (void) didFinishDownload:(id)obj { /* ޙॲཧ */ } @end

Slide 14

Slide 14 text

σϦήʔτ ϝΠϯ Ϋϩʔϥ URL͍ͩ͘͞ ը૾Ͳͧ

Slide 15

Slide 15 text

σϦήʔτ ϝΠϯ Ϋϩʔϥ URL͍ͩ͘͞ ը૾Ͳͧ Vim޷͖ URL͍ͩ͘͞ ը૾Ͳͧ Vim ͱ͔Α͘෼͔Βͳ͍ ͠໘౗ͩͳ ͻΌͬ΄ʔ

Slide 16

Slide 16 text

σϦήʔτ @protocol DLDelegate - (NSURL*)nextURL; - (void)didFinishDownload:(id)object; @end ! @interface MyDownloader : NSObject @property (assign) id delegate @end ! @implementation MyDownloader - (void) downloadAsync { NSURL *url; while(url = [self.delegate nextURL]){ /* μ΢ϯϩʔυ*/ [self.delegate didFinishDownload:obj]; } } @end

Slide 17

Slide 17 text

Φϒαʔόʔ • GoFຊͷObserverύλʔϯ • ΦϒδΣΫτͷΠϯελϯεม਺Λ؂ࢹͯ͠ॲཧΛ ߦ͏

Slide 18

Slide 18 text

Φϒαʔόʔ @implementation Senshi - (id) init { self = [super init]; [self addObserver: self forKeyPath: @“hitPoint” options: NSKeyValueObservingOptionInitial context: NULL]; return self; } ! - (void)observeValueForKeyPath:(NSString *)keyPath… { if ([keyPath isEqual:@“hitPoint”]) { /* Կ͔͢Δ */ } } ! - (void)damage:(NSInteger)damage { self.hitPoint = self.hitPoint - damage; } @end

Slide 19

Slide 19 text

Φϒαʔόʔ @implementation Souryo - (void) mimamoruSenshi:(Senshi*)senshi { [senshi addObserver: self forKeyPath: @“hitPoint” options: NSKeyValueObservingOptionInitial context: NULL]; } ! - (void) observeValueForKeyPath:(NSString *)keyPath… { if ([keyPath isEqual:@“hitPoint”]) { /* ճ෮͢Δ */ } } @end

Slide 20

Slide 20 text

ࢀরΧ΢ϯτ - (void) hello:(NSString*)str { [str retain]; MyObject *obj = [MyObject new]; ! /* Կ͔ॲཧ */ ! [obj release]; [str release]; } ! - (void) helloWorld { NSString *str = @“world”; [self hello:str]; [str release]; } ·͔͞ͷʂʂ ! ࢀরΧ΢ϯτํࣜ ϓϩάϥϛϯά

Slide 21

Slide 21 text

ࢀরΧ΢ϯτ - (void) hello:(NSString*)str { [str retain]; MyObject *obj = [MyObject new]; ! /* Կ͔ॲཧ */ ! [obj release]; [str release]; } ! - (void) helloWorld { NSString *str = @“world”; [self hello:str]; [str release]; } ίϯύΠϥ͕΍ͬͯ͘ΕΔΑ͏ʹͳΓ ·ͨ͠ɻ

Slide 22

Slide 22 text

ࢀরΧ΢ϯτ - (void) hello:(NSString*)str { [str retain]; MyObject *obj = [MyObject new]; ! /* Կ͔ॲཧ */ ! [obj release]; [str release]; } ! - (void) helloWorld { NSString *str = @“world”; [self hello:str]; [str release]; } - (void) hello:(NSString*)str { [str retain]; MyObject *obj = [MyObject new]; ! /* Կ͔ॲཧ */ ! [obj release]; [str release]; } ! - (void) helloWorld { NSString *str = @“world”; [self hello:str]; [str release]; }

Slide 23

Slide 23 text

Cͷ֦ு • Blocks • Grand Central Dispatch

Slide 24

Slide 24 text

BLOCKS // prepare void HelloWorld(void) { printf("Hello, World!\n"); } ! void DoFunc(void (*pfunc)()) { (*pfunc)(); } ! // main void main(void) { DoFunc(HelloWorld); }

Slide 25

Slide 25 text

BLOCKS // prepare void DoBlock(void (^block)(void)) { block(); } ! // main void main(void) { DoBlock(^{ printf(“Hello, World!\n”); }); }

Slide 26

Slide 26 text

BLOCKS // prepare void DoBlock10(void (^block)(int n)) { for (int i=0; i < 10; i++) { block(i); } } ! // main void main(void) { DoBlock10(^(int n){ printf("Hello, Block %d!\n", n); }); }

Slide 27

Slide 27 text

GCD: GRAND CENTRAL DISPATCH “λεΫΛඇಉظʹ࣮ߦ͢Δٕज़ͷͻͱͭͱͯ͠ɺGrand Central DispatchʢGCDʣͱ͍͏΋ͷ͕͋Γ·͢ɻ௨ৗ͸ΞϓϦέʔγϣϯதʹ هड़͢ΔεϨου؅ཧ༻ͷίʔυΛɺγεςϜϨϕϧͰ࣮૷ͨ͠΋ͷͰ ͢ɻ։ൃऀ͕͠ͳ͚Ε͹ͳΒͳ͍ͷ͸ɺ࣮ߦ͍ͨ͠λεΫΛఆٛ͠ɺద ੾ͳDispatch Queueʹ௥Ճ͢Δ͜ͱ͚ͩͰ͢ɻ͢ΔͱGCD͸ɺඞཁͳε ϨουΛੜ੒͠ɺͦ͜ͰλεΫΛ࣮ߦ͢ΔΑ͏ద੾ʹεέδϡʔϦϯά ͠·͢ɻεϨου؅ཧ͕γεςϜͷҰ෦ͱͯ͠૊Έࠐ·Ε͍ͯΔͷͰɺ શମΛݟ౉ͯ͠λεΫΛ؅ཧɺ࣮ߦͰ͖ɺैདྷͷεϨουΑΓ΋ޮ཰͕ ޲্͠·͢ɻ” Excerpt From: “ฒྻϓϩάϥϛϯάΨΠυ” https://developer.apple.com/jp/devcenter/ios/library/ documentation/ConcurrencyProgrammingGuide.pdf

Slide 28

Slide 28 text

GCD // prepare dispatch_queue_priority_t priority; dispatch_queue_t queue; ! priority = DISPATCH_QUEUE_PRIORITY_DEFAULT; queue = dispatch_get_global_queue(priority, 0); ! // main printf(“1,"); ! dispatch_async(queue, ^{ printf("3"); }); printf(“2,”); ! // results => 1,2,3

Slide 29

Slide 29 text

NSOPERATIONQUEUE // https://t.co/6LbHw8F9UU // prepare NSOperationQueue *queue; queue = [NSOperationQueue new]; ! ! ! // main printf(“1,"); ! [queue addOperationWithBlock:^{ printf("3"); }]; printf(“2,”); ! // results => 1,2,3 // prepare dispatch_queue_priority_t priority; dispatch_queue_t queue; ! priority = DISPATCH_QUEUE_PRIORITY_DEFAULT; queue = dispatch_get_global_queue(priority,0); ! // main printf(“1,"); ! dispatch_async(queue, ^{ printf("3"); }); printf(“2,”); ! // results => 1,2,3

Slide 30

Slide 30 text

·ͱΊ • Objective-C ͸Appleઐ༻ͷݴޠͰ͸ͳ͍Α • Blocks ΍ GCD Λ࢖͏ͱ͜Ε·Ͱ໘౗ͩͬͨ C ݴޠ ͷϓϩάϥϛϯά͕Ұ෦ָʹͳΔΑ • Overrideͱ͔͍͢͝ΏΔ͍͔Βڵຯ͋Ε͹Ͳ͏ͧ

Slide 31

Slide 31 text

ࠂ஌ • ΞϓϦϦϦʔε • ΋͘΋͘ձ

Slide 32

Slide 32 text

ΞϓϦϦϦʔεʂ • ΞϓϦΛϦϦʔε͠·ͨ͠ਃ੥தͰ͢ • ࠓ͸ΘΓͱՋͰ͢ • ̍೥ޙʹ࢓ࣄͳ͘ͳΔ͔΋ɻΑΖ͓͘͠ئ͍͠·͢ɻ

Slide 33

Slide 33 text

৽ׁ΋͘΋͘ձ΍ͬͯ·͢ʂ • ݄̏͘Β͍Ͱ౔༵೔ʹʮ৽ׁ΋͘΋͘ձʯ΍ͬͯ· ͢ɻ • ࠷େ໊̔ऩ༰ՄೳɻࢀՃඅແྉʂ • ౔༵೔ʹՈʹ͍ͨ͘ͳ͍ਓ͸ͥͻ • ଞͷ༵೔ͷر๬΋ͥͻ • ษڧձ։͖͍ͨਓ΋͝૬ஊ͍ͩ͘͞

Slide 34

Slide 34 text

͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠