Created
June 3, 2014 10:27
-
-
Save angelworm/d636d06eed3f68c9f6f4 to your computer and use it in GitHub Desktop.
Objective-Cでメソッドチェーンやる奴
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Cocoa/Cocoa.h> | |
@class Hoge; | |
typedef Hoge *(^setter)(int); | |
@interface Hoge : NSObject | |
@property int a,b; | |
@property (nonatomic, readonly, copy) setter setA; | |
@property (nonatomic, readonly, copy) setter setB; | |
@end | |
@implementation Hoge | |
@synthesize a,b; | |
-(id)init | |
{ | |
if(self = [super init]) { | |
self->a = 1; self->b = 2; | |
} | |
return self; | |
} | |
-(setter)setA | |
{ | |
__block __weak Hoge *o = self; | |
return [^Hoge *(int na) { | |
o.a = na; | |
return o; | |
} copy]; | |
} | |
-(setter)setB | |
{ | |
__block __weak Hoge *o = self; | |
return [^Hoge *(int nb) { | |
o.b = nb; | |
return o; | |
} copy]; | |
} | |
@end | |
int main() | |
{ | |
Hoge *obj = [[Hoge alloc] init]; | |
NSLog(@"a = %d, b = %d", obj.a, obj.b); | |
obj.setA(23).setB(56); | |
NSLog(@"a = %d, b = %d", obj.a, obj.b); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment