c 方法
NSString *updateQuery(NSString *url, NSString *key, NSString *value)
{
if (isBlankString(url)) {
return @"";
}
NSURLComponents *components = [NSURLComponents componentsWithString:url];
NSMutableArray *tmpQueryItems = [NSMutableArray arrayWithArray:components.queryItems];
[tmpQueryItems enumerateObjectsUsingBlock:^(NSURLQueryItem *obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj.name isEqualToString:key]) {
NSURLQueryItem *tmpQueryItem = [[NSURLQueryItem alloc] initWithName:key value:value];
[tmpQueryItems replaceObjectAtIndex:idx withObject:tmpQueryItem];
}
}];
components.queryItems = tmpQueryItems;
return components.string;
}
object-c 方法
- (NSString *)updateUrl:(NSString *)url
key:(NSString *)key
value:(NSString *)value
{
NSURLComponents *components = [NSURLComponents componentsWithString:url];
NSMutableArray *tmpQueryItems = [NSMutableArray arrayWithArray:components.queryItems];
[tmpQueryItems enumerateObjectsUsingBlock:^(NSURLQueryItem *obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj.name isEqualToString:key]) {
NSURLQueryItem *tmpQueryItem = [[NSURLQueryItem alloc] initWithName:key value:value];
[tmpQueryItems replaceObjectAtIndex:idx withObject:tmpQueryItem];
}
}];
components.queryItems = tmpQueryItems;
return components.string;
}
|