Created
June 21, 2013 06:16
-
-
Save zhangxigithub/5829239 to your computer and use it in GitHub Desktop.
解析NSURL中GET传数值的参数
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // NSURL+ZXURL.h | |
| // ZXTools | |
| // | |
| // Created by zhangxi on 13-6-21. | |
| // Copyright (c) 2013年 张玺. All rights reserved. | |
| // | |
| /* | |
| demo | |
| NSURL *url = [NSURL URLWithString:@"http://zhangxi.me/?name=cc&birth=6.12"]; | |
| NSLog(@"%@",[url parameters]); | |
| NSLog(@"%@",[url parameterForKey:@"name"]); //cc | |
| NSLog(@"%@",[url parameterForKey:@"sex"]); //(null) | |
| NSLog(@"%@",[url parameterForKey:@"birth"]); //6.12 | |
| */ | |
| #import <Foundation/Foundation.h> | |
| @interface NSURL (ZXURL) | |
| -(NSString *)parameterForKey:(NSString *)key; | |
| -(NSDictionary *)parameters; | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // NSURL+ZXURL.m | |
| // ZXTools | |
| // | |
| // Created by zhangxi on 13-6-21. | |
| // Copyright (c) 2013年 张玺. All rights reserved. | |
| // | |
| #import "NSURL+ZXURL.h" | |
| @implementation NSURL (ZXURL) | |
| -(NSString *)parameterForKey:(NSString *)key | |
| { | |
| NSDictionary *parameters = [self parameters]; | |
| if(parameters == nil) | |
| return nil; | |
| return parameters[key]; | |
| } | |
| -(NSDictionary *)parameters | |
| { | |
| NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; | |
| NSString *query = [self query]; | |
| NSArray *values = [query componentsSeparatedByString:@"&"]; | |
| if(values == nil || values.count == 0) | |
| return nil; | |
| for(NSString *value in values) | |
| { | |
| NSArray *param = [value componentsSeparatedByString:@"="]; | |
| if(param == nil || param.count !=2) | |
| continue; | |
| [parameters setObject:param[1] forKey:param[0]]; | |
| } | |
| return parameters; | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment