标签: nsurlcomponents

URLComponents.url是零

我正在尝试用它URLComponents来组成一个URL看起来像它的东西.

但是,当我访问url组件的属性时,它是零.

例...

var urlComponents = URLComponents(string: "http://google.com")!
urlComponents.path = "auth/login"
Run Code Online (Sandbox Code Playgroud)

然后我做......

print(urlComponents)
Run Code Online (Sandbox Code Playgroud)

输出...

scheme: http host: google.com path: auth/login
  - scheme : "http"
  - host : "google.com"
  - path : "auth/login"
Run Code Online (Sandbox Code Playgroud)

但是之后...

print(urlComponents.url)
Run Code Online (Sandbox Code Playgroud)

输出nil.

我做错了吗?如何从这一切中获取完整形成的URL?谢谢

nsurl ios swift nsurlcomponents

27
推荐指数
2
解决办法
4622
查看次数

What kind of URL is not conforming to RFC 3986 but is conforming to RFC 1808, RFC 1738, and RFC 2732?

The doc of URLComponents.init(url:resolvingAgainstBaseURL:) says:

Returns the initialized URL components object, or nil if the URL could not be parsed.

Knowing that:

我假设URLComponents当 URL 符合 RFC 1808/1738/2732 但不符合 RFC 3986 时, 的初始化将失败。那是什么样的 URL?有什么例子吗?

到目前为止,我唯一的提示可能与不同的保留字符有关?

url nsurl rfc3986 swift nsurlcomponents

4
推荐指数
1
解决办法
679
查看次数

Swift Components.Url 返回错误的 URL

我实现了以下代码,我可以在其中传递资源名称,它应该为我提供 URL。我正在使用 Xcode 14 Beta 3。

 static let baseUrl = "localhost:8080"
 static func resource(for resourceName: String) -> URL? {
            
            var components = URLComponents()
            components.scheme = "http"
            components.percentEncodedHost = baseUrl
            components.path = "/\(resourceName)"
            return components.url
            
        }
Run Code Online (Sandbox Code Playgroud)

我将资源名称传递为“my-pets”,它应该返回,http://localhost:8080/my-pets但它不断返回http://my-pets。我不确定我在哪里犯了错误。

xcode swift nsurlcomponents xcode14

3
推荐指数
1
解决办法
995
查看次数

为什么NSURL在方案之后使用双正斜杠解析字符串与具有单个正斜杠的字符串不同?

将字符串解析为NSURL对象时,NSURL使用单个正斜杠将字符串处理为与方案后面带有双正斜杠的字符串不同.

为什么会这样?

以下是一些例子:

NSURL *url = [NSURL URLWithString:@"app-id://path1/path2"];

NSLog(@"%@",url.scheme);    // "app-id"
NSLog(@"%@",url.path);      // "/path2"
NSLog(@"%@",url.host);      // "path1"


NSURL *url = [NSURL URLWithString:@"app-id:/path1/path2"];

NSLog(@"%@",url.scheme);    // "app-id"
NSLog(@"%@",url.path);      // "/path1/path2"
NSLog(@"%@",url.host);      // nil
Run Code Online (Sandbox Code Playgroud)

cocoa nsurl core-foundation ios nsurlcomponents

2
推荐指数
1
解决办法
978
查看次数

带 url 参数的 Swift GET 请求

我正在构建一项调查,当用户回答问题时,我将答案附加到带有参数的 url 字符串中,并向我的服务器发送 get 请求。因此,对于每个答案,都会记录所选答案、时间戳和调查的唯一 ID。

我不确定最好的方法,但这就是我到目前为止所拥有的。

我创建了 url 和查询项。

var urlComponents: URLComponents {

    let resultID = surveyQuestions.resultId
    print("\(String(describing: resultID))")

    let resultResponseID = surveyQuestions.questions[surveyResultResponseId]
    print("\(String(describing: resultResponseID))")

    let questionIndex = questionNumbers
    print("\(String(describing: questionIndex))")

    var urlComponents = URLComponents(string: "My String URL")

    urlComponents?.queryItems = [
        URLQueryItem(name: "surveyResultsId", value: "\(String(describing: resultID))"),
        URLQueryItem(name: "surveyResultsResponseId", value: "\(String(describing: resultResponseID))"),
        URLQueryItem(name: "questions", value: "\(questionIndex)"),
        URLQueryItem(name: "selectedAnswer", value: "\(storedAnswer)")

    ] 

    let url = urlComponents?.url

    print(url!.absoluteString as Any)

    return urlComponents!
}
Run Code Online (Sandbox Code Playgroud)

然后我构建发送请求。

func sendRequest(_ url: String, parameters: [String: String], completion: …
Run Code Online (Sandbox Code Playgroud)

get-request swift nsurlcomponents swift4

2
推荐指数
1
解决办法
9382
查看次数

如何防止 URLComponents().port 在查询前添加问号 (Swift/Xcode)

我正在尝试URLComponents()在我设计的应用程序中组成一个代表。

这是代码:

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    var components = URLComponents()

    components.scheme = "http"
    components.host = "0.0.0.0"
    components.port = 9090
    let queryItemToken = URLQueryItem(name: "/predict?text", value: "what's your name?")
    components.queryItems = [queryItemToken]

    print(components.url as Any)
    }
}
Run Code Online (Sandbox Code Playgroud)

这是上述代码段的输出:

Optional(http://0.0.0.0:9090?/predict?text=what's%20your%20name?)
Run Code Online (Sandbox Code Playgroud)

由于 ? 在端口和查询之间!我怎样才能防止URLComponents()插入这个多余的?在端口和查询之间!

目标输出: Optional(http://0.0.0.0:9090/predict?text=what's%20your%20name?)

swift nsurlcomponents

0
推荐指数
1
解决办法
1003
查看次数

避免在 URLComponents 中对主机进行百分比编码

使用URLComponents,有没有办法避免主机的百分比编码?

var components = URLComponents()
components.scheme = "https"
components.host = "endpoint.com/v1/api.php?"
// ends up as https://endpoint.com%2Fv1%2Fapi.php%3F? but I want it to stay as https://endpoint.com/v1/api.php?
Run Code Online (Sandbox Code Playgroud)

这个问题是关于避免编码,而不是添加编码作为作为重复状态链接的问题。

ios swift nsurlcomponents

-1
推荐指数
1
解决办法
487
查看次数