示例函数
/**
处理Get请求URL
@param url 请求链接
@param params 请求参数
@return 请求链接
*/
- (NSString *)getURLForInterfaceStringDefine:(NSString *)url
params:(NSArray<NSString *> *)params {
// 判空
if (!url && !url.length) {
return @"";
}
NSString *reg = @"\\{.+?\\}";
// 创建
NSString *resultUrl = url;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:reg options:NSRegularExpressionCaseInsensitive error:nil];
NSArray<NSTextCheckingResult *> *matchesArr = [regex matchesInString:url options:0 range:NSMakeRange(0, url.length)];
// 匹配项与参数不同的时候,返回原字符串
if (matchesArr.count == params.count) {
for (NSInteger i = 0; i < params.count; i++) {
NSTextCheckingResult *current = [regex firstMatchInString:resultUrl options:0 range:NSMakeRange(0, resultUrl.length)];
resultUrl = [resultUrl stringByReplacingCharactersInRange:current.range withString:[params objectAtIndex:i]];
}
}
return resultUrl;
}
示例调用
NSString *originalStr = @"/api/aaa/aaa/aaaa/aaaa/aa/{fundID}/{Ruldid}/{fundid}/delete.action";
NSArray<NSString *> * params = @[@"1",@"2",@"3"];
NSString *url = [HttpUtil getURLForInterfaceStringDefine:originalStr params:params];
NSLog(@"%@",url);