iOS: parse a URL into segments

前端 未结 4 442
闹比i
闹比i 2020-12-02 08:41

What\'s an efficient way to take an NSURL object such as the following:

foo://name/12345 

and break it up into one string and one unsigned

相关标签:
4条回答
  • 2020-12-02 09:12

    NSURL has a method pathComponents, which returns an array with all the different path components. That should help you get the integer part. To get the name I'd use the host method of the NSURL. The docs say, that it should work if the URL is properly formatted, might as well give it a try then.

    All in all, no need to convert into a string, there seems to be plenty of methods to work out the components of the URL from the NSURL object itself.

    0 讨论(0)
  • 2020-12-02 09:26

    I can only add an example here, the NSURL class is the one to go. This is not complete but will give you a hint on how to use NSURL:

    NSString *url_ = @"foo://name.com:8080/12345;param?foo=1&baa=2#fragment";
    NSURL *url = [NSURL URLWithString:url_];
    
    NSLog(@"scheme: %@", [url scheme]); 
    NSLog(@"host: %@", [url host]); 
    NSLog(@"port: %@", [url port]);     
    NSLog(@"path: %@", [url path]);     
    NSLog(@"path components: %@", [url pathComponents]);        
    NSLog(@"parameterString: %@", [url parameterString]);   
    NSLog(@"query: %@", [url query]);       
    NSLog(@"fragment: %@", [url fragment]);
    

    output:

    scheme: foo
    host: name.com
    port: 8080
    path: /12345
    path components: (
        "/",
        12345
    )
    parameterString: param
    query: foo=1&baa=2
    fragment: fragment
    

    This Q&A NSURL's parameterString confusion with use of ';' vs '&' is also interesting regarding URLs.

    0 讨论(0)
  • 2020-12-02 09:28

    Thanks to Nick for pointing me in the right direction.

    I wanted to compare file urls but was having problems with extra slashes making isEqualString useless. You can use my example below for comparing two urls by first de-constructing them and then comparing the parts against each other.

    - (BOOL) isURLMatch:(NSString*) url1 url2:(NSString*) url2
    {
        NSURL *u1 = [NSURL URLWithString:url1];
        NSURL *u2 = [NSURL URLWithString:url2];
    
        if (![[u1 scheme] isEqualToString:[u2 scheme]]) return NO;
        if (![[u1 host] isEqualToString:[u2 host]]) return NO;
        if (![[url1 pathComponents] isEqualToArray:[url2 pathComponents]]) return NO;
    
        //check some properties if not nil as isEqualSting fails when comparing them
        if ([u1 port] && [u2 port])
        {
            if (![[u1 port] isEqualToNumber:[u2 port]]) return NO;
        }
    
        if ([u1 query] && [u2 query])
        {
            if (![[u1 query] isEqualToString:[u2 query]]) return NO;
        }
        return YES;
    }  
    
    0 讨论(0)
  • 2020-12-02 09:30

    Actually there is a better way to parse NSURL. Use NSURLComponents. Here is a simle example:

    Swift:

    extension URL {
        var params: [String: String]? {
            if let urlComponents = URLComponents(url: self, resolvingAgainstBaseURL: true) {
                if let queryItems = urlComponents.queryItems {
                    var params = [String: String]()
                    queryItems.forEach{
                        params[$0.name] = $0.value
                    }
                    return params
                }
            }
            return nil
        }
    }
    

    Objective-C:

    NSURLComponents *components = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO];
        NSArray *queryItems = [components queryItems];
    
        NSMutableDictionary *dict = [NSMutableDictionary new];
    
        for (NSURLQueryItem *item in queryItems)
        {
            [dict setObject:[item value] forKey:[item name]];
        }
    
    0 讨论(0)
提交回复
热议问题