LoginSignup
0
0

【Swift】URLSession.shared.dataTask() をうまく使いこなせない

Posted at

以下、参考にしたい新しいAPIのサンプルコードです。

// Fetch photo with async/await

func fetchPhoto(url: URL) async throws -> UIImage {
  let (data, response) = try await URLSession.shared.data(from: url)

  guard let httpResponse = response as? HTTPURLResponse,
        httpResponse.statusCode == 200 else {
    throw WoofError.invalidServerResponse
  }

  guard let image = UIImage(data: data) else {
    throw WoofError.unsupportedImage
  }
  return image
}

時間の無駄なく将来のある王道な本筋のコードを吸収していきたいです。

👉 【Swift】URLSession.shared.dataTask() をうまく使いこなせない hatena-bookmark

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0