Best way to parse URL string to get values for keys?

前端 未结 16 1092
广开言路
广开言路 2020-11-29 18:36

I need to parse a URL string like this one:

&ad_eurl=http://www.youtube.com/video/4bL4FI1Gz6s&hl=it_IT&iv_logging_level=3&ad_flags=0&ends         


        
16条回答
  •  孤独总比滥情好
    2020-11-29 19:16

    Since iOS 8 you can directly use properties name and value on NSURLQueryItem.

    Example, how to parse URL and get specific values for a key in parsed pairs.

    NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:@"someURL" resolvingAgainstBaseURL:false];
    NSArray *queryItems = urlComponents.queryItems;
    NSMutableArray *someIDs = [NSMutableArray new];
    for (NSURLQueryItem *item in queryItems) {
        if ([item.name isEqualToString:@"someKey"]) {
            [someIDs addObject:item.value];
        }
    }
    NSLog(@"%@", someIDs);
    

提交回复
热议问题