answer by Abdelahad Darwishは改善され、おそらくあなたの特定の問題(違法な文字)を修正しますが、一般的に、これらの種類のエラーを検出し、アプリをクラッシュさせるリスクの代わりに優雅に失敗したいと考えています。
var urlString: String = "https://website.com/åbo_beach.jpg".stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!
guard let url = URL(string: urlString) else {
// String can not yield a valid URL; do someting!
return
}
URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) -> Void in
// ...
}).resume()
:
特に、(すなわち、潜在的に失敗する可能性)、代わりに、必要なチェックを追加する任意の値を返すメソッドに!
から離れ