// // FRPString.h // xxxxxDemo // // Created by xxxxxx on 2020/11/22. // #import <Foundation/Foundation.h> NS_ASSUME_NONNULL_BEGIN @interface FRPString : NSObject @property (nonatomic,strong)NSMutableString *sourceString; + (NSString *)FRPStringOperate:(void(^)(FRPString *fpr))operate; - (FRPString *(^)(NSString *))append; - (FRPString *(^)(NSString *))remove; @end NS_ASSUME_NONNULL_END
// // FRPString.m // xxxxxDemo // // Created by xxxxxx on 2020/11/22. // #import "FRPString.h" @implementation FRPString + (NSString *)FRPStringOperate:(void(^)(FRPString *fpr))operate { FRPString *frpObj = [[FRPString alloc]init]; frpObj.sourceString = [NSMutableString string]; if(operate) operate(frpObj); return frpObj.sourceString.copy; } - (FRPString *(^)(NSString *))append { FRPString *(^adBlock)(NSString *) = ^FRPString* (NSString *string){ [self.sourceString appendString:string]; NSLog(@"%@",self.sourceString); return self; }; return adBlock; } - (FRPString *(^)(NSString *))remove { FRPString *(^rmBlock)(NSString *) = ^FRPString* (NSString *string){ if([self.sourceString rangeOfString:string].location != NSNotFound){ [self.sourceString deleteCharactersInRange:[self.sourceString rangeOfString:string]]; NSLog(@"%@",self.sourceString); } return self; }; return rmBlock; } @end
调用:
// [FRPString FRPStringOperate:^(FRPString * _Nonnull fpr) { fpr.append(@"this is my first string by add").remove(@"add"); }]; //类似于 FRPString *frp = [[FRPString alloc]init]; frp.sourceString = [NSMutableString string]; //返回block FRPString *(^addBlock)(NSString *) = [frp append]; //通过返回的block,返回了FRPString对象 FRPString *adFrp = addBlock(@"添加一些字符串"); FRPString *(^rmBlock)(NSString *) = [adFrp remove]; FRPString *rmFrp = rmBlock(@"添加"); //点语法是直接调用方法所返回的block,并且为其block进行传参 // //或 frp.append(@"是block吗?").remove(@"吗?"); //