将Objective-C语法简化到极致,从此不再烦恼!!!

最近又发现好玩的东西了,你猜OC语法能简化到什么程度???

TokenBlockExtension

This categories may keep you away from the fussy style of Objective-C!
Without the bracket,more easier

Before

    //字符串.....
    NSString *test = @"this is the old way!";
    NSString *string = [NSString stringWithFormat:@"%@",@"The old way may waste you the precious time"];
    NSString *newString = [string stringByReplacingOccurrencesOfString:@"old" withString:@"new"];
    newString = [newString stringByReplacingOccurrencesOfString:@"waste" withString:@"save"];
    

    NSString *names = @"Bob";
    names = [NSString stringWithFormat:@"%@,Jack",names];
    names = [NSString stringWithFormat:@"%@,Seven",names];
    names = [NSString stringWithFormat:@"%@,Hannah",names];
    
    
    UIButton *testButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [testButton setFrame:CGRectMake(0, 0, 200, 200)];
    [testButton addTarget:self action:@selector(btn1Pressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:testButton];
    [testButton setTitle:@"old" forState:(UIControlStateNormal)];
    [testButton setTitle:@"fussy" forState:(UIControlStateHighlighted)];
    UIColor *color = [UIColor colorWithRed:200/255 green:200/255 blue:200/255 alpha:1];
    [testButton setTintColor:color];

Now

    NSString *newTest = @"this is the old way!";
    newTest = newTest.token_replace(@"old", @"new")
                     .token_replace(@"waste", @"save");
    
    NSString *newNames = @"Bob".token_append(@"Jack").token_append(@"Seven").token_append(@"Hannah");
    
    //新建一个UIButton
    UIButton *newButton = UIButton.token_buttonWithType(UIButtonTypeCustom)
                                  .token_addTarget(self, @selector(btn1Pressed:), UIControlEventTouchUpInside)
                                  .token_setTitleWithState(@"old",UIControlStateNormal)
                                  .token_setTitleWithState(@"fussy",UIControlStateHighlighted)
                                  .token_setTintColor(UIColor.token_RGB(200,200,200))
                                  .token_setFrame(CGRectMake(0, 0, 200, 200));
    self.view.token_addSubview(newButton);

    //发送一个网络请求
    TokenNetworking.networking()
    .sendRequest(^NSURLRequest *(TokenNetworking *assistant) {
        return NSMutableURLRequest.token_requestWithURL(@"https://www.baidu.com")
        .token_setMethod(@"GET")
        .token_setUA(UA)
        .token_handleCookie(NO)
        .token_addHeaderValues(@{@"Cookie":cookie});
    })
    .transform(^id(TokenNetworking *networking ,id responsedObj) {
        return [networking HTMLTextSerializeWithData:responsedObj];
    }).finish(^(TokenNetworking *netWorking,NSURLSessionTask *task, id responsedObj) {
        !success?:success(responsedObj);
    }, ^(TokenNetworking *netWorkingObj, NSError *error) {
        !failure?:failure(NSError.token_netError);
    });
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 10,131评论 0 23
  • 以後別做朋友,朋友不能牽手 想愛你的衝動,我衹能一笑帶過 “那時候如果知道是這樣的結果,你是否還會選擇這麼做?” ...
    闔秋阅读 311评论 0 2