从URL中截取所包含的参数,并且以字典的形式返回和参数字典转UR
2019-05-29 本文已影响0人
杂货铺学徒
- (NSString *)keyValueStringWithDict:(NSDictionary *)dict
{
if (dict == nil) {
return nil;
}
NSMutableString *string = [NSMutableString stringWithString:@"?"];
[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
[string appendFormat:@"%@=%@&",key,obj];
}];
if ([string rangeOfString:@"&"].length) {
[string deleteCharactersInRange:NSMakeRange(string.length - 1, 1)];
}
return string;
}
-(NSDictionary *)dictionaryWithUrlString:(NSString *)urlStr
{
if (urlStr && urlStr.length && [urlStr rangeOfString:@"?"].length == 1) {
NSArray *array = [urlStr componentsSeparatedByString:@"?"];
if (array && array.count == 2) {
NSString *paramsStr = array[1];
if (paramsStr.length) {
NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
NSArray *paramArray = [paramsStr componentsSeparatedByString:@"&"];
for (NSString *param in paramArray) {
if (param && param.length) {
NSArray *parArr = [param componentsSeparatedByString:@"="];
if (parArr.count == 2) {
[paramsDict setObject:parArr[1] forKey:parArr[0]];
}
}
}
return paramsDict;
}else{
return nil;
}
}else{
return nil;
}
}else{
return nil;
}
}