32

ひろ子 in Objective-C

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: ひろ子 in Objective-C
Page 2: ひろ子 in Objective-C

who is ひろ子 ?

Page 3: ひろ子 in Objective-C
Page 4: ひろ子 in Objective-C
Page 5: ひろ子 in Objective-C

void hiroko(int height) { if(height < 180) height = 180; }

正しくない hiroko の実装

Page 6: ひろ子 in Objective-C

void hiroko(int *height) { if(*height < 180) *height = 180; }

正しい hiroko の実装(多分

Page 7: ひろ子 in Objective-C

int h1 = 195; hiroko(&h1); NSLog(@"h1: %d", h1);

Page 8: ひろ子 in Objective-C

int h1 = 195; hiroko(&h1); NSLog(@"h1: %d", h1);

2014-05-09 14:09:40.299 Hiroko[23593:60b] h1: 195

理想通りなのでOK

Page 9: ひろ子 in Objective-C

int h1 = 195; hiroko(&h1); NSLog(@"h1: %d", h1);

2014-05-09 14:09:40.299 Hiroko[23593:60b] h1: 195

int h2 = 175; hiroko(&h2); NSLog(@"h2: %d", h2);

Page 10: ひろ子 in Objective-C

int h1 = 195; hiroko(&h1); NSLog(@"h1: %d", h1);

2014-05-09 14:09:40.301 Hiroko[23593:60b] h2: 180

2014-05-09 14:09:40.299 Hiroko[23593:60b] h1: 195

int h2 = 175; hiroko(&h2); NSLog(@"h2: %d", h2);

ちゃんと超能力が働いてる

Page 11: ひろ子 in Objective-C

解説

int h = 175; !int *p; p = &h; *p = 180; !NSLog(@"h: %d", h);

Page 12: ひろ子 in Objective-C

解説

int h = 175; !int *p; p = &h; *p = 180; !NSLog(@"h: %d", h);

175

h: 0xbfffca44

Page 13: ひろ子 in Objective-C

解説

int h = 175; !int *p; p = &h; *p = 180; !NSLog(@"h: %d", h);

175

h: 0xbfffca44

NULL

p: 0xbfffca40

Page 14: ひろ子 in Objective-C

解説

int h = 175; !int *p; p = &h; *p = 180; !NSLog(@"h: %d", h);

175

h: 0xbfffca44

0xbfffca44

p: 0xbfffca40

Page 15: ひろ子 in Objective-C

解説

int h = 175; !int *p; p = &h; *p = 180; !NSLog(@"h: %d", h);

180

h: 0xbfffca44

0xbfffca44

p: 0xbfffca40

Page 16: ひろ子 in Objective-C

解説

int h = 175; !int *p; p = &h; *p = 180; !NSLog(@"h: %d", h);

2014-05-09 14:09:40.299 Hiroko[23593:60b] h: 180

Page 17: ひろ子 in Objective-C

ひろ子パターン:「どうぞ書き換えて下さい」

Page 18: ひろ子 in Objective-C

ひろ子 in Objective-C?

Page 19: ひろ子 in Objective-C

@interface Hiroko : NSObject !- (void)doHirokoWithHeight:(CGFloat *)height; !@end

…?

Page 20: ひろ子 in Objective-C

Objective-C (Cocoa) では、あまりひろ子パターンは出て来ない。

Page 21: ひろ子 in Objective-C

NSArray *array = @[@"a", @"b", @"c"]; ![array enumerateObjectsUsingBlock: ^(id obj, NSUInteger idx, BOOL *stop) { ! NSLog(@"%@", obj); if([obj isEqual:@"b"]) *stop = YES; }]; !NSLog(@"end");

ひろ子パターン その1: - [NSArray enumerateObjectsUsingBlock:]

Page 22: ひろ子 in Objective-C

NSArray *array = @[@"a", @"b", @"c"]; ![array enumerateObjectsUsingBlock: ^(id obj, NSUInteger idx, BOOL *stop) { ! NSLog(@"%@", obj); if([obj isEqual:@"b"]) *stop = YES; }]; !NSLog(@"end");

ひろ子パターン その1: - [NSArray enumerateObjectsUsingBlock:]

Page 23: ひろ子 in Objective-C

NSArray *array = @[@"a", @"b", @"c"]; ![array enumerateObjectsUsingBlock: ^(id obj, NSUInteger idx, BOOL *stop) { ! NSLog(@"%@", obj); if([obj isEqual:@"b"]) *stop = YES; }]; !NSLog(@"end");

ひろ子パターン その1: - [NSArray enumerateObjectsUsingBlock:]

Page 24: ひろ子 in Objective-C

NSArray *array = @[@"a", @"b", @"c"]; ![array enumerateObjectsUsingBlock: ^(id obj, NSUInteger idx, BOOL *stop) { ! NSLog(@"%@", obj); if([obj isEqual:@"b"]) *stop = YES; }]; !NSLog(@"end");

2014-05-09 14:50:04.508 Hiroko[24612:60b] a 2014-05-09 14:50:04.509 Hiroko[24612:60b] b 2014-05-09 14:50:04.509 Hiroko[24612:60b] end

ひろ子パターン その1: - [NSArray enumerateObjectsUsingBlock:]

Page 25: ひろ子 in Objective-C

NSData *data = ...; !NSError *error = nil; !id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; if(json && !error) { ... }

ひろ子パターン その2: NSError ** として

Page 26: ひろ子 in Objective-C

NSData *data = ...; !NSError *error = nil; !id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; if(json && !error) { ... }

ひろ子パターン その2: NSError ** として

nil

NSError *error NSError

Page 27: ひろ子 in Objective-C

NSData *data = ...; !NSError *error = nil; !id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; if(json && !error) { ... }

ひろ子パターン その2: NSError ** として

nil

NSError *error

0xf8193ab

NSError **errorPtr NSError

引数として渡される

Page 28: ひろ子 in Objective-C

NSData *data = ...; !NSError *error = nil; !id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; if(json && !error) { ... }

ひろ子パターン その2: NSError ** として

nil

NSError *error

0xf8193ab

NSError **errorPtr

(オブジェクトの実体)

NSError

エラー発生!

Page 29: ひろ子 in Objective-C

NSData *data = ...; !NSError *error = nil; !id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; if(json && !error) { ... }

ひろ子パターン その2: NSError ** として

0xf19274a

NSError *error

0xf8193ab

NSError **errorPtr

(オブジェクトの実体)

NSError

代入されて処理が戻る

Page 30: ひろ子 in Objective-C

NSData *data = ...; !NSError *error = nil; !id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; if(json && !error) { ... }

ひろ子パターン その2: NSError ** として

0xf19274a

NSError *error

(オブジェクトの実体)

NSError

↑ nil でなくなっているので通らない

Page 31: ひろ子 in Objective-C

Objective-C でもひろ子はたまに出てくる。

Page 32: ひろ子 in Objective-C

Thanks.