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()
Dieser Code sollte kompilieren (vorausgesetzt, 'user.image' hat den Typ' String? '). Bitte zeigen Sie [mcve], um das Problem zu demonstrieren. –
Ich habe meinen Beitrag aktualisiert –
Nochmal: Dieser Code * kompiliert. * –