2017-02-14 23 views
0

У меня есть следующий код, но я получаю сообщение об ошибке.URL (строка :) Невозможно вызвать значение нефункционного типа 'String'

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

Как я могу создать URL? URL (строка :) предполагается взять строку, не так ли? И вот что такое 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() 
+0

Этот код должен компилироваться (при условии, что 'user.image' имеет тип' String? '). Пожалуйста, покажите [mcve], демонстрируя проблему. –

+0

Я обновил сообщение –

+0

Снова: этот код * компилируется. * –

ответ

0

Была та же проблема, до сих пор не мог понять весь путь. Ощущается как Xcode 8 ошибка, потому что в соответствии с сайта яблока, он должен работать (ссылка: Apple's URL reference)

в любом случае, я использовал обходной путь для этого:

let imageUrl = NSURL(string: "www.apple.com") as! URL 
1

Вам нужно сделать объект URL-адрес, подобный этому

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

 Смежные вопросы

  • Нет связанных вопросов^_^