URL(string:) 无法调用非函数类型“字符串"的值 [英] URL(string:) Cannot call value of non-function type 'String'

查看:32
本文介绍了URL(string:) 无法调用非函数类型“字符串"的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the follow code but I am getting an error.

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

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

How am I supposed to create url? URL(string:) is supposed to take a String isn't it? And that's what userImageURL is.

这是我试图实现的代码示例.即使在这个例子中,对于 catPictureURL

Here's an example of the code I'm trying to implement. Even in this example, I'm getting the same error for 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的对象

You need to make object of URL like this

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

这篇关于URL(string:) 无法调用非函数类型“字符串"的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆