Contents

Develop
2014.05.12 16:43

[ios] URL 파라미터 파싱~

조회 수 4563 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
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
번호 분류 제목 글쓴이 날짜 조회 수
711 Develop [ios] How To Use UIScrollView to Scroll and Zoom Content (Using Objective-C) file hooni 2016.03.23 4246
710 Develop 알고리즘 성능분석 file hooni 2014.06.24 4264
709 Develop [c#]뉴 툴바 개인적으로 만든거.. (old) secret hooni 2013.04.23 4272
708 Develop [ios] CoreData 사용하기 (튜토리얼) hooni 2014.03.28 4314
707 Develop [java] netty (비동기 이벤트 방식 네트워크 프레임워크) 사용법 #1 (server) 1 hooni 2015.01.02 4315
706 Develop [swift] popToRoot 모달뷰, 네비게이션컨트롤러 한꺼번에 닫기 file hooni 2021.01.29 4376
705 Develop [ios] Swift 4 String, Date, DateFormatter 예제 hooni 2018.10.18 4394
704 Develop [js] Click button copy to clipboard hooni 2018.04.05 4398
703 Develop [iOS] Xcode 불필요한 캐시 삭제하기 hooni 2021.10.12 4403
702 Develop [ios] 유용한 매크로 hooni 2014.03.26 4427
701 Develop [ios] UIWebView 캐쉬 삭제 hooni 2014.04.08 4432
700 Develop [android] 딜레이를 구현하기 위한 꼼수 hooni 2016.11.24 4446
Board Pagination Prev 1 ... 7 8 9 10 11 12 13 14 15 16 ... 71 Next
/ 71