gpt4 book ai didi

swift - 网址(字符串 :) Cannot call value of non-function type 'String'

转载 作者:行者123 更新时间:2023-12-01 00:36:13 26 4
gpt4 key购买 nike

我有以下代码,但出现错误。

if let userImageURL = user.image {
let url = URL(string: userImageURL)
}

我应该如何创建网址? URL(string:)应该带一个 String不是吗?这就是 userImageURL是。

编辑:这是我试图实现的代码示例。即使在这个例子中,我也遇到了同样的错误 catPictureURL
let catPictureURL = URL(string: "http://i.imgur.com/w5rkSIj.jpg")!

// Creating a session object with the default configuration.
// You can read more about it here https://developer.apple.com/reference/foundation/urlsessionconfiguration
let session = URLSession(configuration: .default)

// Define a download task. The download task will download the contents of the URL as a Data object and then you can do what you wish with that data.
let downloadPicTask = session.dataTask(with: catPictureURL) { (data, response, error) in
// The download has finished.
if let e = error {
print("Error downloading cat picture: \(e)")
} else {
// No errors found.
// It would be weird if we didn't have a response, so check for that too.
if let res = response as? HTTPURLResponse {
print("Downloaded cat picture with response code \(res.statusCode)")
if let imageData = data {
// Finally convert that Data into an image and do what you wish with it.
let image = UIImage(data: imageData)
// Do something with your image.
} else {
print("Couldn't get image: Image is nil")
}
} else {
print("Couldn't get response code for some reason")
}
}
}

downloadPicTask.resume()

最佳答案

您需要像这样制作 URL 对象

let url = Foundation.URL(string:"your_url_string")

关于swift - 网址(字符串 :) Cannot call value of non-function type 'String' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42218883/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com