NSURL 为参数字符串中的键提取单个值 [英] NSURL pull out a single value for a key in a parameter string

查看:46
本文介绍了NSURL 为参数字符串中的键提取单个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 NSURL:

I have an NSURL:

serverCall?x=a&y=b&z=c

serverCall?x=a&y=b&z=c

获得 y 值的最快和最有效的方法是什么?

What is the quickest and most efficient way to get the value of y?

谢谢

推荐答案

更新:

自 2010 年撰写本文以来,Apple 似乎为此发布了一套工具.请参阅下面的答案.

Since 2010 when this was written, it seems Apple has released a set of tools for that purpose. Please see the answers below for those.

老式解决方案:

好吧,我知道你说过最快的方法",但在我开始使用 NSScanner 进行测试后,我就是停不下来.虽然这不是最短的方法,但如果您打算大量使用该功能,它肯定会很方便.我创建了一个 URLParser 类,它使用 NSScanner 获取这些变量.使用很简单:

Well I know you said "the quickest way" but after I started doing a test with NSScanner I just couldn't stop. And while it is not the shortest way, it is sure handy if you are planning to use that feature a lot. I created a URLParser class that gets these vars using an NSScanner. The use is a simple as:

URLParser *parser = [[[URLParser alloc] initWithURLString:@"http://blahblahblah.com/serverCall?x=a&y=b&z=c&flash=yes"] autorelease];
NSString *y = [parser valueForVariable:@"y"];
NSLog(@"%@", y); //b
NSString *a = [parser valueForVariable:@"a"];
NSLog(@"%@", a); //(null)
NSString *flash = [parser valueForVariable:@"flash"];
NSLog(@"%@", flash); //yes

执行此操作的类如下(*帖子底部的源文件):

And the class that does this is the following (*source files at the bottom of the post):

URLParser.h

@interface URLParser : NSObject {
    NSArray *variables;
}

@property (nonatomic, retain) NSArray *variables;

- (id)initWithURLString:(NSString *)url;
- (NSString *)valueForVariable:(NSString *)varName;

@end

URLParser.m

@implementation URLParser
@synthesize variables;

- (id) initWithURLString:(NSString *)url{
    self = [super init];
    if (self != nil) {
        NSString *string = url;
        NSScanner *scanner = [NSScanner scannerWithString:string];
        [scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"&?"]];
        NSString *tempString;
        NSMutableArray *vars = [NSMutableArray new];
        [scanner scanUpToString:@"?" intoString:nil];       //ignore the beginning of the string and skip to the vars
        while ([scanner scanUpToString:@"&" intoString:&tempString]) {
            [vars addObject:[tempString copy]];
        }
        self.variables = vars;
        [vars release];
    }
    return self;
}

- (NSString *)valueForVariable:(NSString *)varName {
    for (NSString *var in self.variables) {
        if ([var length] > [varName length]+1 && [[var substringWithRange:NSMakeRange(0, [varName length]+1)] isEqualToString:[varName stringByAppendingString:@"="]]) {
            NSString *varValue = [var substringFromIndex:[varName length]+1];
            return varValue;
        }
    }
    return nil;
}

- (void) dealloc{
    self.variables = nil;
    [super dealloc];
}

@end

*如果你不喜欢复制和粘贴,你可以下载源文件 - 我写了一篇关于这个的快速博客文章 此处.

*if you don't like copying and pasting you can just download the source files - I made a quick blog post about this here.

这篇关于NSURL 为参数字符串中的键提取单个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆