ひろ子 in objective-c

Post on 27-Aug-2014

4.069 Views

Category:

Software

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

 

TRANSCRIPT

who is ひろ子 ?

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

正しくない hiroko の実装

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

正しい hiroko の実装(多分

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

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

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

理想通りなのでOK

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);

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);

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

解説

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

解説

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

175

h: 0xbfffca44

解説

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

175

h: 0xbfffca44

NULL

p: 0xbfffca40

解説

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

175

h: 0xbfffca44

0xbfffca44

p: 0xbfffca40

解説

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

180

h: 0xbfffca44

0xbfffca44

p: 0xbfffca40

解説

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

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

ひろ子 in Objective-C?

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

…?

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

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:]

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:]

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:]

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:]

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

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

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

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

nil

NSError *error NSError

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

引数として渡される

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

エラー発生!

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

代入されて処理が戻る

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

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

0xf19274a

NSError *error

(オブジェクトの実体)

NSError

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

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

Thanks.

top related