將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...
    又是夜半驚坐起閱讀 9,787評論 0 23
  • 以後別做朋友,朋友不能牽手 想愛你的衝動,我衹能一笑帶過 “那時候如果知道是這樣的結果,你是否還會選擇這麼做?” ...
    闔秋閱讀 304評論 0 2