parsingNSURLpath和查询(iphoneOS)
有没有标准的对象或函数来parsing一个NSURL的组件? 显然我可以写一个,但为什么重新发明轮子?
[NSURL path]
会返回一个NSString
比如"argX=x&argY=y&argZ=z"
我想回来的是一个字典,里面填充了{@"argX" => @"x", @"argY" => @"y", @"argZ" = @"z"}
对于返回像“/ partA / partB / partC”这样的string的path,我宁愿得到一个结构为{[0] => @"partA", [1] => @"partB", [2] => @"partC"}
我意识到这是一个非常具体的问题,但它似乎是很多人想要的东西…
编辑 – 这是为iPhoneOS! 显然NSURL在Mac OS上有不同的function
您可能需要查看pathComponents
,它返回URL组件的数组。 在这里获取更多信息。
好吧,我得到了ntsi,并写了一个解决scheme,通过类别扩展NSString。 我还没有testing过,但如果你想使用它,就去做吧。
@interface NSString (ParseCategory) - (NSMutableDictionary *)explodeToDictionaryInnerGlue:(NSString *)innerGlue outterGlue:(NSString *)outterGlue; @end @implementation NSString (ParseCategory) - (NSMutableDictionary *)explodeToDictionaryInnerGlue:(NSString *)innerGlue outterGlue:(NSString *)outterGlue { // Explode based on outter glue NSArray *firstExplode = [self componentsSeparatedByString:outterGlue]; NSArray *secondExplode; // Explode based on inner glue NSInteger count = [firstExplode count]; NSMutableDictionary *returnDictionary = [NSMutableDictionary dictionaryWithCapacity:count]; for (NSInteger i = 0; i < count; i++) { secondExplode = [(NSString *)[firstExplode objectAtIndex:i] componentsSeparatedByString:innerGlue]; if ([secondExplode count] == 2) { [returnDictionary setObject:[secondExplode objectAtIndex:1] forKey:[secondExplode objectAtIndex:0]]; } } return returnDictionary; } @end
这就是这样调用的:
NSMutableDictionary *parsedQuery = [[myNSURL query] explodeToDictionaryInnerGlue:@"=" outterGlue=@"&"]
为了parsingNSURL的path部分(即@“/ partA / partB / partC”),只需调用:
NSArray *parsedPath = [[nyNSURL path] componentsSeperatedByString:@"/"];
请注意,parsedPath [0]将是一个空string,因为前导/!
编辑 – 这是NSURL的类别扩展为您的使用乐趣。 它剥离了最初的“/”,所以你没有一个空的0索引。
@implementation NSURL (ParseCategory) - (NSArray *)pathArray { // Create a character set for the slash character NSRange slashRange; slashRange.location = (unsigned int)'/'; slashRange.length = 1; NSCharacterSet *slashSet = [NSCharacterSet characterSetWithRange:slashRange]; // Get path with leading (and trailing) slashes removed NSString *path = [[self path] stringByTrimmingCharactersInSet:slashSet]; return [path componentsSeparatedByCharactersInSet:slashSet]; } - (NSDictionary *)queryDictionary { NSDictionary *returnDictionary = [[[[self query] explodeToDictionaryInnerGlue:@"=" outterGlue:@"&"] copy] autorelease]; return returnDictionary; } @end
Sam Soffes为NSURL / NSDictionary创build了一个维护良好的类别。 它可以在这里find: https : //github.com/samsoffes/sstoolkit/
下面是我用来parsing查询string
// Parse the individual parameters // parameters = @"hello=world&foo=bar"; NSMutableDictionary *dictParameters = [[NSMutableDictionary alloc] init]; NSArray *arrParameters = [parameters componentsSeparatedByString:@"&"]; for (int i = 0; i < [arrParameters count]; i++) { NSArray *arrKeyValue = [[arrParameters objectAtIndex:i] componentsSeparatedByString:@"="]; if ([arrKeyValue count] >= 2) { NSMutableString *strKey = [NSMutableString stringWithCapacity:0]; [strKey setString:[[[arrKeyValue objectAtIndex:0] lowercaseString] stringByReplacingPercentEscapesUsingEncoding: NSUTF8StringEncoding]]; NSMutableString *strValue = [NSMutableString stringWithCapacity:0]; [strValue setString:[[[arrKeyValue objectAtIndex:1] stringByReplacingOccurrencesOfString:@"+" withString:@" "] stringByReplacingPercentEscapesUsingEncoding: NSUTF8StringEncoding]]; if (strKey.length > 0) [dictParameters setObject:strValue forKey:strKey]; } } NSLog(@"Parameters: %@", dictParameters);
如果你决定写一个( 我不确定是否有现成的方法来获取你想要的组件 ),你可能想使用NSString的componentsSeparatedByString
。
在iOS 8中引入,OS X 10.10是NSURLQueryItem
,可用于构build查询。 从NSURLQueryItem上的文档:
NSURLQueryItem对象表示URL的查询部分中的项目的单个名称/值对。 您使用具有NSURLComponents对象的queryItems属性的查询项目。
您可以通过首先创buildNSURLComponents
从URL中检索查询项目:
NSURL *url = [NSURL URLWithString:@"http://stackoverflow.com?q=ios&count=10"]; NSURLComponents *components = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:YES]; for (NSURLQueryItem *item in components.queryItems) { NSLog(@"name: %@, value: %@", item.name, item.value); } // name: q, value: ios // name: count, value: 10
请注意,它们返回值为-queryItems
是一个数组,而不是一个字典。 这是因为以下是有效的url。 注意两个相同的“键”, foo
。
http://google.com?foo=bar&foo=baz
要通过查询项创buildURL,请使用指定的初始化程序queryItemWithName:value:
然后将它们添加到NSURLComponents
以生成NSURL
。 例如:
NSString *urlString = @"http://stackoverflow.com"; NSURLComponents *components = [NSURLComponents componentsWithString:urlString]; NSURLQueryItem *search = [NSURLQueryItem queryItemWithName:@"q" value:@"ios"]; NSURLQueryItem *count = [NSURLQueryItem queryItemWithName:@"count" value:@"10"]; components.queryItems = @[ search, count ]; NSURL *url = components.URL; // http://stackoverflow.com?q=ios&count=10
请注意,问号和&符号会自动处理。 从参数字典中创build一个NSURL
非常简单:
NSDictionary *queryDictionary = @{ @"q": @"ios", @"count": @"10" }; NSMutableArray *queryItems = [NSMutableArray array]; for (NSString *key in queryDictionary) { NSURLQueryItem *item = [NSURLQueryItem queryItemWithName:key value:queryDictionary[key]]; [queryItems addObject:item]; } components.queryItems = queryItems;
我还写了一篇更详细的博客文章, 使用NSURLQueryItems构buildNSURL 。