Develop
2014.05.12 16:43

[ios] URL 파라미터 파싱~

Views 3670 Votes 0 Comment 0
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
URL 파라미터 파싱하는..

URLParser.h
@interface URLParser : NSObject

@property (nonatomic, retain) NSArray *variables;
@property (nonatomic, retain) NSArray *keys;

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

@end

URLParser.m
@implementation URLParser

- (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 *keys = [NSMutableArray new];
        NSMutableArray *vars = [NSMutableArray new];

        //ignore the beginning of the string and skip to the vars
        [scanner scanUpToString:@"?" intoString:nil];

        while ([scanner scanUpToString:@"&" intoString:&tempString]) {
            [vars addObject:[tempString copy]];
            
            [keys addObject:
                [[tempString componentsSeparatedByString:@"="]
                    objectAtIndex:0]];
        }
        self.variables = vars;
        self.keys = keys;
    }
    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;
    self.keys = nil;
}

@end

?

List of Articles
No. Category Subject Author Date Views
357 Develop [html] HTML5 튜토리얼 링크 ㅋㅋ hooni 2013.04.23 12813
356 System/OS [linux] 콘솔 기본언어 설정 방법 hooni 2013.04.23 12826
355 System/OS [sql] 내 방명록 답글 찾는 쿼리문.. (JOIN 구문) hooni 2003.04.23 12845
354 System/OS [linux] ProFTPD 타임아웃 설정 hooni 2003.04.23 12851
353 Develop [doc] UI개발시 유용한 소프트웨어 (개발 및 디버깅 툴) hooni 2013.04.23 12909
352 Develop [c] 다중연결 서버 만들기 #1 - fork() 사용 file hooni 2013.04.23 12918
351 System/OS [router] 시스코 라우터 명령어 모드.. hooni 2013.04.23 12944
350 System/OS [linux] 아파치 설치/설정(proxy) hooni 2003.04.23 12965
349 Develop [js] 자바스크립트의 클로저 (JavaScript's Closure) hooni 2013.05.15 12977
348 Develop [php] 심플한 게시판 ㅋㅋ 1 file hooni 2013.04.23 12987
347 Develop [js] AngularJS를 소개합니다. file hooni 2014.01.06 13014
346 System/OS [linux] ipfwadm를 이용한 패킷필터링(구버전) hooni 2003.04.23 13017
345 System/OS [linux] Proftpd 설치 가이드 hooni 2003.04.23 13021
344 System/OS [linux] DHCP(Dynamic Host Configuration Protocol) 서버 hooni 2003.04.23 13023
343 Develop [html] 캐쉬된 웹페이지 사용하지 않도록 하는 방법 hooni 2003.04.23 13028
342 System/OS [linux] 한글 URL 인식할 수 있게 아파치(Apache) 설정 (mod_url.c 설치) hooni 2013.04.23 13060
Board Pagination Prev 1 ... 50 51 52 53 54 ... 74 Next
/ 74