&
您可以使用and拆分从 URL 返回的查询=
,并将它们放入字典中。
NSURL *url = [NSURL URLWithString:@"myApp://?q=200"];
NSArray *query = [[url query] componentsSeparatedByString:@"&"];
NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithCapacity:[query count]];
for(NSString *parameter in query)
{
NSArray *kv = [parameter componentsSeparatedByString:@"="];
[parameters setObject:[kv count] > 1 ? [[kv objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSISOLatin1StringEncoding] : [NSNull null]
forKey:[[kv objectAtIndex:0] stringByReplacingPercentEscapesUsingEncoding:NSISOLatin1StringEncoding]];
}
NSLog(@"Parameters: %@", parameters);
NSLog(@"q = %@", [parameters objectForKey:@"q"]);
在此示例中,如果参数没有值,我只需将其设置为NSNull
. 这意味着您需要检查NSNull
或更改逻辑以跳过带有值的键或将它们设置为空字符串。