美文网首页
block实现链式编程

block实现链式编程

作者: Z了个L | 来源:发表于2016-11-13 21:09 被阅读24次

// Person.h
#import <Foundation/Foundation.h>

@interface Person : NSObject
//- (Person *)study;
//- (Person *)run;

- (Person *(^)(NSString *name))study;
- (Person *(^)())run;
@end


// Person.m
#import "Person.h"

@implementation Person
//- (Person *)study
//{
//    NSLog(@"study----");
//
//    return self;
//}
//
//- (Person *)run
//{
//    NSLog(@"run----");
//    return self;
//}

- (Person *(^)(NSString *))study
{
    return ^(NSString *name){
        NSLog(@"study----%@", name);
        return self;
    };
}

- (Person *(^)())run
{
    return ^{
        NSLog(@"run----");
        return self;
    };
}
@end


// main.m
#import <Foundation/Foundation.h>
#import "Person.h"

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        Person *p = [[Person alloc] init];

//        [p run];
//        [p study];
//        [[p run] study];
//        [[[[p run] study] run] study];

        p.study(@"Math").run().study(@"English");
    }
    return 0;
}



相关文章

网友评论

      本文标题:block实现链式编程

      本文链接:https://www.haomeiwen.com/subject/ffwcpttx.html