iOS:将 URL 解析为段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5903157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:49:18  来源:igfitidea点击:

iOS: parse a URL into segments

iphoneobjective-cios

提问by randombits

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

获取 NSURL 对象的有效方法是什么,例如:

foo://name/12345 

and break it up into one string and one unsigned integer, where the string val is 'name' and the unsigned int is 12345?

并将其分解为一个字符串和一个无符号整数,其中字符串 val 是 'name' 而无符号整数是 12345?

I'm assuming the algorithm involves converting NSURL to an NSString and then using some components of NSScanner to finish the rest?

我假设该算法涉及将 NSURL 转换为 NSString,然后使用 NSScanner 的某些组件来完成其余的工作?

采纳答案by Henri Normak

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 hostmethod of the NSURL. The docs say, that it should work if the URL is properly formatted, might as well give it a try then.

NSURL 有一个方法pathComponents,它返回一个包含所有不同路径组件的数组。这应该可以帮助您获得整数部分。要获得名称,我将使用hostNSURL的方法。文档说,如果 URL 格式正确,它应该可以工作,那么不妨尝试一下。

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.

总而言之,无需转换为字符串,似乎有很多方法可以从 NSURL 对象本身计算出 URL 的组件。

回答by Nick Weaver

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

我只能在这里添加一个例子,NSURL类就是一个。这不是完整的,但会给你一个关于如何使用 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.

此问答NSURL 的参数字符串与“;”的使用混淆 vs '&'在 URL 方面也很有趣。

回答by Andrei Malygin

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

实际上有更好的方法来解析 NSURL。使用 NSURLComponents。这是一个简单的例子:

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[
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]];
    }
.name] =
- (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;
}  
.value } return params } } return nil } }

Objective-C:

目标-C:

##代码##

回答by Anthony De Souza

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.

我想比较文件 url,但遇到额外的斜杠问题,使 isEqualString 无用。您可以使用我下面的示例来比较两个 url,方法是首先解构它们,然后将各个部分相互比较。

##代码##