Contents

Develop
2014.05.12 16:43

[ios] URL 파라미터 파싱~

Views 3738 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
349 Develop [c] 단기과정[01/08] 과제.. 파스칼 삼각형 file hooni 2003.04.23 7297
348 Develop [c] 신기한 atoi함수(www.game79.net) hooni 2003.04.23 7291
347 Develop [c++] mfc로 만든 부엌 수납 시스템(2D기반 설계) file hooni 2013.04.23 7289
346 Develop [c] 패킷정보출력(경로, 패킷길이, 3hand, sync, ack..) file hooni 2003.04.23 7286
345 Develop [c] 무선 Radius Server 자료.. file hooni 2013.04.23 7284
344 Develop [php] 논문 관리 프로그램.. ㅋㅋ file hooni 2013.04.23 7284
343 Develop [chm] Programming Applications for Microsoft Windows file hooni 2013.04.23 7282
342 Develop [linux] crond 사용법.. ㅋㅋ hooni 2013.04.23 7277
341 Develop [c++] mfc로 만든 인테리어 수납 시스템(2D기반 설계) file hooni 2013.04.23 7276
340 Develop [c] 단기과정[01/08] 배열, 포인터 hooni 2003.04.23 7267
339 Develop [php] 웹 터미널 & 업로드 소스.. file hooni 2013.04.23 7261
338 Develop [c] 단기과정[01/15] 피보나치, 파스칼.. 링크리스트 file hooni 2003.04.23 7259
Board Pagination Prev 1 ... 37 38 39 40 41 42 43 44 45 46 ... 71 Next
/ 71