2017-02-14 2 views
0

Ich habe den folgenden Code, aber ich bekomme einen Fehler.URL (string :) Kann nicht Wert von Nicht-Funktion Typ 'String'

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

Wie soll ich URL erstellen? URL (string :) soll einen String aufnehmen, nicht wahr? Und das ist userImageURL.

bearbeiten: Hier ist ein Beispiel für den Code, den ich versuche zu implementieren. Auch in diesem Beispiel, erhalte ich den gleichen Fehler für 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

Dieser Code sollte kompilieren (vorausgesetzt, 'user.image' hat den Typ' String? '). Bitte zeigen Sie [mcve], um das Problem zu demonstrieren. –

+0

Ich habe meinen Beitrag aktualisiert –

+0

Nochmal: Dieser Code * kompiliert. * –

Antwort

0

Wurde das gleiche Problem, kann immer noch nicht es heraus ganz heraus. Man fühlt sich wie ein Xcode 8 Fehler, weil nach der Apple-Website sollte es funktionieren (Referenz: Apple's URL reference)

sowieso, habe ich eine Abhilfe für dieses:

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

Sie müssen Objekt machen von URL wie diesem

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