Getting Data From API in Swift + iOS (Xcode 15 tutorial) - Beginners

iOS Academy · 97K views · 2020 · 9:50

Open on YouTube

Software (other)Web developmenttutorial

Why Stack Overflow recommends it

Stack Overflow users link it as the example they followed when learning to call a web API and decode JSON in Swift, usually when their own decoding code then fails.

Recommended by 0 people across 0 different months, 2021 to 2022.

What it is

A beginner-friendly iOS tutorial from iOS Academy that shows how to fetch data from a remote API in Swift using URLSession. It then uses the Codable protocol and JSONDecoder to turn the returned JSON into Swift types, working in Xcode 15.

What people said

Cites John Sorensen · 2021-10 · Stack Overflow · on “Swift JSON decoding url”

I'm starting to hook up my frontend SwiftUI project to a backend and so I'm learning how to make API calls in Swift. I began by trying to use the code in this video [this video] as a workable example. However, I'm running into an error where the JSONDecoder is failing to decode the returned data. What is going on here? My apologies for the blunt question--I'm new to backend development. let url = " [LINK] " getData(from: url) private func getData(from url: String) { let task = URLSession.shared.dataTask(with: URL(string: url)!, completionHandler: { data, response, error in guard let data = data, error == nil else { print("Something Went Wrong...") return } // data is available var result: Respons

Cites PMaier · 2022-01 · Stack Overflow · on “Parse Wiki Nearby”

I'm new to Swift, and trying to figure out how to parse the JSON returned by Wiki Nearby. With the help of [this video] here's where I am right now: import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. let url = " [LINK] " getData(from: url) } private func getData(from url: String) { let task = URLSession.shared.dataTask(with: URL(string: url)!, completionHandler: {data, response, error in guard let data = data, error == nil else { print("Something went wrong") return } var result: Response? do { result = try JSONDecoder().decode(Response.self, from: data) } catch { print("failed to convert \(err

Cites emiliorivas16 · 2022-05 · Stack Overflow · on “The data couldn't be read because it isn't in the correct format”

Hello I am creating an app with Xcode and I am having the following problem, I created this API with mockapi.io (if you enter the link you'll see the JSON data) [LINK] If you dont want to enter the link here is how it looks the JSON: (By default the JSON has an array without name as the root and that can't be modified) [ { "categorie":"Fruits", "id":"1" }, { "categorie":"Animals", "id":"2" }, { "categorie":"Vegetables", "id":"3" }, { "categorie":"Juices", "id":"4" }, { "categorie":"Alcohol", "id":"5" }, { "categorie":"Desserts", "id":"6" } ] The problem I have is that when I try to decode the data from the API it cant't be readed because is in the wrong format, I am trying to recreate the sa

Cites CoderER · 2022-05 · Stack Overflow · on “The given data was not valid JSON, Can't read the JSON from API properly”

I am creating an app with Xcode and I am having the following problem, I created this API (if you enter the link you'll see the JSON data) [LINK] If you dont want to enter the link here is how this si how the structure of the JSON looks like: { "items":[ { "categorie":"Fruits", "id_categorie":"1" }, { "categorie":"Animals", "id_categorie":"2" }, { "categorie":"Juices", "id_categorie":"3" }, { "categorie":"Vegetables", "id_categorie":"4" }, { "categorie":"Alcohol", "id_categorie":"5" }, { "categorie":"Desserts", "id_categorie":"6" } ] } The problem I have is that when I try to decode the data from the API it cant't be decoded properly, I am trying to recreate the same code of this youtube vid