SwiftUI API / JSON Tutorial with Async Await Example

แชร์
ฝัง
  • เผยแพร่เมื่อ 19 เม.ย. 2022
  • This SwiftUI API / JSON / Networking tutorial walks you through how to properly use swiftui json parsing after fetching the data through an API. We create a struct and use swiftui codable, along with async / await to fill our list with the parsed data. SwiftUI is one of the best ways for beginners to learn how to create their own app in iOS / Swift. You will need Xcode to complete this tutorial.
    We use a breaking bad API that can be found here:
    Want to see more?
    The Story Behind My New App: • I Turned This Life-Cha...
    How I'd Learn to Code Today: • How a Senior iOS Engin...
    RevenueCat / In-App Purchases Tutorial: • Easily Make Money with...
    Follow me and say hello!
    Instagram: / logankoshenka
    Twitter: / logankoshenka​
    #SwiftUI #API #JSON #Tutorial #Networking
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 125

  • @morelogankoshenka
    @morelogankoshenka  2 ปีที่แล้ว +14

    I recently hit 800 subscribers - you guys are awesome!

    • @karljay7473
      @karljay7473 2 ปีที่แล้ว +1

      Thanks for the tutorial! Can you post a link to the source code? I know it's not a lot of code, but I'm using Goodnotes on an iPad to make summaries of tutorials so I can keep track of all the tutorials and I import PDFs of the code, the mark it up with notes.

    • @rachelradford5193
      @rachelradford5193 2 ปีที่แล้ว +1

      @@karljay7473 Idk if it helps but you could take a pic and import it onto a page in GoodNotes and make notes around it.

    • @karljay7473
      @karljay7473 2 ปีที่แล้ว +2

      @@rachelradford5193 Thanks, that's what I ended up doing. Worked out pretty good.

    • @DianaFlores-H
      @DianaFlores-H ปีที่แล้ว +1

      thanks for the video, I´m from México, and i want to learn more about swiftUI

    • @morelogankoshenka
      @morelogankoshenka  ปีที่แล้ว +1

      that’s great!!!

  • @rachelradford5193
    @rachelradford5193 2 ปีที่แล้ว +10

    Idk who you are but I've watched a million tutorials about making async/await calls in swift, with varying degrees of success. I started doubting my life as a developer because it was so hard to understand, especially, coming from JavaScript. Idk why others feel its so necessary to over complicate things but I am so thankful for this video that I randomly found on a reddit thread!!

    • @morelogankoshenka
      @morelogankoshenka  2 ปีที่แล้ว +2

      so awesome to hear - glad you liked it!!!

    • @rachelradford5193
      @rachelradford5193 2 ปีที่แล้ว +1

      @@morelogankoshenka can you tell me why tis doesnt work?
      import SwiftUI
      import Foundation
      struct Trivia: Codable, Hashable {
      var category: String
      var type: String
      var difficulty: String
      var question: String
      //var isCorrect: Bool
      //var correct_answer: String
      ///var incorrect_answers: [String]
      }
      struct TriviaApi: View {
      @State private var trivia = [Trivia]()
      var body: some View {
      NavigationView {
      List(trivia, id: \.self) { trivia in
      VStack(alignment: .leading) {
      Text(trivia.question)
      .font(.headline)
      .foregroundColor(Color.blue)
      }
      }.navigationTitle("Trivia")
      .task {
      await fetch()
      }
      }
      }
      func fetch() async {
      guard let url = URL(string: "opentdb.com/api.php?amount=10") else { return print("Missing URL")}
      do {
      let (data, _) = try await URLSession.shared.data(from: url)
      if let decodedResponse = try? JSONDecoder().decode([Trivia].self, from: data) {
      trivia = decodedResponse
      }
      }catch {
      print("Error")
      }
      }
      }
      struct TriviaApi_Previews: PreviewProvider {
      static var previews: some View {
      TriviaApi()
      }
      }
      If not, It's ok!!

    • @morelogankoshenka
      @morelogankoshenka  2 ปีที่แล้ว

      instead of printing “error” in the catch statement in your fetch function, remove the quotes and call: print(error) … there is a built in error variable there that will be available if it gets to that point and you should be able to get more of an idea of what is going wrong … the majority of my issues w this has been with the URL or the decoding type!

    • @rachelradford5193
      @rachelradford5193 2 ปีที่แล้ว +1

      @@morelogankoshenka Thanks for answering! Unfortunately that didn't help me out. I have seen ppl use .utf8 encoding in their API calls, I might try that out?? Swift has been so fun but there are some really funky problems I've come across. I really hope they continue to improve async/await. Thanks again!

  • @buresdv
    @buresdv 2 ปีที่แล้ว +2

    This tutorial is truly amazing. I couldn't wrap my head around the async/await pattern no matter how many articles for "Swift beginners" I've read, but this video finally made it click. Thank you very much for the awesome tutorial :)

  • @chrisking821
    @chrisking821 9 หลายเดือนก่อน +3

    Just thought id help the future Swift learners out there: this was the simplest and only tutorial that actually worked for my api.

    • @iLoveAppl3947
      @iLoveAppl3947 6 หลายเดือนก่อน

      yeah but don't use this tutorial as an example for the real world applications. He doesn't handle errors and response like it should , doesn't follow MVVM and many more...

    • @morelogankoshenka
      @morelogankoshenka  6 หลายเดือนก่อน

      this is a tutorial, meant to introduce people to async await, made when async / await came out … you are correct in that error handling is necessary in a real world application, and there are plenty of resources available out there … as far as MVVM, there is no one correct design pattern for SwiftUI or all of iOS for that matter … this is a quick, easy async / await tutorial, that’s it … a specific design pattern is irrelevant here, as several could be used depending on the scenario

  • @RamazanOzerr
    @RamazanOzerr 3 หลายเดือนก่อน +1

    I have been watching many tutors on youtube and bro you are the funniest, now Im gonna have to watch all of your videos.. thank you bruther :D

  • @mahtabhasan8513
    @mahtabhasan8513 ปีที่แล้ว +1

    Simple and nice way of explaining Async/await, thanks Logan
    Appreciate your regular working on Swiftui, helped me a lot.

  • @alexdiaz1712
    @alexdiaz1712 2 ปีที่แล้ว +2

    I always look forward to your videos 😆 Thank you for the great tutorial!

  • @samtree99
    @samtree99 ปีที่แล้ว +1

    Excellent video. As a retired C++ programmer, following ur example, I was able to make an app for my day trading needs. Thank you so much. Your code is short and precise, right at the points. You must be a great engineer!

  • @mattmarshall1834
    @mattmarshall1834 2 ปีที่แล้ว +1

    Another great, to the point, explanation. Please keep making these, they're great.

  • @rungxanh2901
    @rungxanh2901 2 ปีที่แล้ว +1

    Very intuitive and fun Logan! Much appreciated brother 🙏

  • @johnpill1
    @johnpill1 2 ปีที่แล้ว +1

    Great short tutorial. I expanded on it by adding in a page displaying characters with names and clipped images.

  • @deiv319x
    @deiv319x 4 หลายเดือนก่อน +1

    Man I hope you remember me from a previous comment, I’ve been binge watching your videos and they are hilarious (and useful), I’m laughing at every video ending 😂

    • @morelogankoshenka
      @morelogankoshenka  4 หลายเดือนก่อน +1

      hahah I appreciate it man!! thanks for letting me know 🤝

  • @shinygoldfat
    @shinygoldfat 2 ปีที่แล้ว +1

    omg this is soooo clearly explained. I love you

  • @evennorthug2585
    @evennorthug2585 2 ปีที่แล้ว +1

    Good drive and explanations. Keep it up!

  • @wevertonsantiago4305
    @wevertonsantiago4305 2 ปีที่แล้ว +1

    Thanks so much! News subs here! Your videos are perfect 🙂👍

  • @ignaciobrambilla7126
    @ignaciobrambilla7126 2 ปีที่แล้ว +1

    Great tutorial! Thanks!!!

  • @ronpeschel85
    @ronpeschel85 ปีที่แล้ว +1

    "cause I tried it" hahahahah love the channel man keep up this amazing and informative content :))

  • @codingkim5714
    @codingkim5714 2 ปีที่แล้ว +1

    Awesome! I’d love to your simple example for API call. Thanks a lot!!

  • @saadsiddiqui9151
    @saadsiddiqui9151 ปีที่แล้ว +1

    Thankyou so much bro! I am a react native developer and just started learning SwiftUI, been looking for a solution to fetch data from an API but the nasty errors in xcode did not leave me alone. This solution worked well!!

  • @whistlerbrad
    @whistlerbrad ปีที่แล้ว +1

    Such a good tutorial 👏 entertaining as well

  • @nimor2967
    @nimor2967 2 ปีที่แล้ว +1

    Very good thank you.

  • @jayvalance
    @jayvalance 2 ปีที่แล้ว +1

    Great!! Thx 🙏🏼

  • @umedsabzaev6142
    @umedsabzaev6142 2 ปีที่แล้ว +1

    thank you very much. this video is beneficial for beginners like me 😋👍

  • @danuff
    @danuff 11 หลายเดือนก่อน

    One more question, where would you put .resume if one had too? Thanks again.

  • @appfree2u907
    @appfree2u907 2 ปีที่แล้ว +1

    how to setup admob to swiftui?

  • @AnWithTheDrew
    @AnWithTheDrew 2 ปีที่แล้ว +1

    Bro ...can u do more videos cuz I like your presentation. And easy to understand. I really like it bro. Keep Going ❤️

  • @TheAshokWB
    @TheAshokWB 2 ปีที่แล้ว +1

    Thank YOu

  • @tarun_reddy
    @tarun_reddy ปีที่แล้ว

    This was so tuff

  • @bocetta8936
    @bocetta8936 2 ปีที่แล้ว +1

    I love your videos... please, make video to explain how use Google IPA with Oauth 2.0

  • @HanjayaWijangga
    @HanjayaWijangga ปีที่แล้ว +1

    i have error like this "Info.plist contained no UIScene configuration dictionary (looking for configuration named "(no name)")" anyone know why ??

    • @morelogankoshenka
      @morelogankoshenka  ปีที่แล้ว +1

      hmm I'm not sure what this could be but if I'm able to find an answer I'll reply here.

  • @ecptavares
    @ecptavares 11 หลายเดือนก่อน +1

    Nice video thank you.What if I have a JSON file with all my data can I upload it to a server and how would I read the data from this file and not from an api site? thank u

    • @morelogankoshenka
      @morelogankoshenka  11 หลายเดือนก่อน

      you should be able to use the URL to your server! if not, reading the JSON file locally is always an option too!

  • @thomasanderson1416
    @thomasanderson1416 ปีที่แล้ว +1

    Canvas Simulator in XCode is so unnervingly slow, I wonder why they don’t include the Swift Playgrounds previewer in XCode.
    It’s so much faster.

  • @danuff
    @danuff ปีที่แล้ว +1

    Hi....Fantastic Tutorial. I'm new to APIs. One quick question, why didn't you include the .resume in the code? Doesn't async need it? Thanks.

    • @morelogankoshenka
      @morelogankoshenka  ปีที่แล้ว

      Yes haha I probably should have … in most instances you’ll be using .resume so it would’ve made sense to show a scenario where you’d need it. My bad!

  • @reece5863
    @reece5863 ปีที่แล้ว

    Thanks for the code, really needed this. Though the api is kaput.

  • @BlackLightning0Games
    @BlackLightning0Games 2 ปีที่แล้ว +1

    Awesome tutorial. Can you make a video on custom line charts?
    It would be cool to show how to load data as the user drags the chart to the left or right.

    • @morelogankoshenka
      @morelogankoshenka  2 ปีที่แล้ว +1

      Thanks! And yes - I want to look into the new SwiftUI Charts capabilities!

  • @victorriurean
    @victorriurean ปีที่แล้ว

    nice

  • @swaragandhi6122
    @swaragandhi6122 ปีที่แล้ว

    Your video is amazing. I have a doubt though. How to limit number of quotes based on input from textfield?

    • @morelogankoshenka
      @morelogankoshenka  ปีที่แล้ว +1

      for limiting number of quotes, you could do List or ForEach(0…10) for example

    • @swaragandhi6122
      @swaragandhi6122 ปีที่แล้ว

      @@morelogankoshenka thank you 😊

    • @morelogankoshenka
      @morelogankoshenka  ปีที่แล้ว +1

      no problem!

  • @im_the_raymond
    @im_the_raymond 2 ปีที่แล้ว +1

    Good job dude!, im new fan :)
    Can you do video about testing “swift playground 4” app ? It’s on ipad and mac.

  • @nhermuhlen
    @nhermuhlen ปีที่แล้ว +1

    Hey! question how can i reload the data every 1 minute? And by the way good video learned a lot from it.

    • @morelogankoshenka
      @morelogankoshenka  ปีที่แล้ว

      Thanks! You’d have to use some sort of timer … I think if you do a couple timer tutorials, combine what you learned here & w those, and you’ll be able to repeat a task for as long / often as you’d like! But yeah, look into the Timer type.

  • @gouthamshiv
    @gouthamshiv 2 ปีที่แล้ว +1

    Thank you for sharing this content, simple and to the point.
    Can you please let me know how to pass header values ?

    • @morelogankoshenka
      @morelogankoshenka  2 ปีที่แล้ว

      thank you! can you elaborate a little more? what do you mean header values?

    • @gouthamshiv
      @gouthamshiv 2 ปีที่แล้ว +1

      @@morelogankoshenka a http header, like an api-key?

    • @morelogankoshenka
      @morelogankoshenka  2 ปีที่แล้ว +1

      ahhh ok let me look into it

    • @gouthamshiv
      @gouthamshiv 2 ปีที่แล้ว +1

      @@morelogankoshenka I’m actually a FullStack developer, but I’m just couple of days old in the realm of iOS development 😅. I have some APIs to call, but they require an API Key to be passed as part of the header.
      I saw some other references, but I really liked your coding style (to the point & no unnecessary complexity) and I wanted to know the simple and right way from you.
      If possible, can add on what is the best practice to place such keys? In a normal web app, it’ll be injected during the build process

    • @morelogankoshenka
      @morelogankoshenka  2 ปีที่แล้ว +1

      love it! and typically in my experiences you’ll see certain APIs requiring keys, and you’ll just plug that in during configuration or when you’re setting up your URL … I usually just store that as a variable and plug it in as needed

  • @blackmoses4083
    @blackmoses4083 2 ปีที่แล้ว +1

    How do you decode a single object and not an array of objects?

    • @morelogankoshenka
      @morelogankoshenka  2 ปีที่แล้ว +1

      Your api endpoint will have to point to a specific object, usually done via a unique ID, and then you just change the @State variable and the array you’re decoding into that single type

    • @blackmoses4083
      @blackmoses4083 2 ปีที่แล้ว +1

      @@morelogankoshenka How exactly would I declare it as a single type?
      @State private var pets = Pets ?

    • @morelogankoshenka
      @morelogankoshenka  2 ปีที่แล้ว +1

      @State private var pet = Pet … then you won’t be able to use a list, so maybe just display 1 text on the screen since you’ll only be getting a single object

    • @blackmoses4083
      @blackmoses4083 2 ปีที่แล้ว +1

      ​@@morelogankoshenka Can't I use a ForEach loop interchangeably with a List? But yea my state variable didn't work , I was met with a "Expected member name or constructor call after type name" error

    • @morelogankoshenka
      @morelogankoshenka  2 ปีที่แล้ว

      Yeah you could make a list just repeat the same thing if you want … it may have to be var pet: Pet or var pet = Pet() instead

  • @jerrynkongolo
    @jerrynkongolo ปีที่แล้ว +1

    Thanks for the tutorial but for some reason there is no error but nothing shows up

    • @morelogankoshenka
      @morelogankoshenka  ปีที่แล้ว +1

      a few of the comments mentioned that the API was discontinued … I would say to take the structure & concepts from this tutorial and use it for other APIs!

    • @jerrynkongolo
      @jerrynkongolo ปีที่แล้ว +1

      @@morelogankoshenka Thank you for the prompted reply. I will keep you posted. Thanks again for the amazing word your way of explaining is so easy to follow. I am looking forward to more tutorials

    • @morelogankoshenka
      @morelogankoshenka  ปีที่แล้ว

      thanks so much!

  • @ashin6651
    @ashin6651 2 ปีที่แล้ว +1

    nice nice nice

  • @markme7092
    @markme7092 2 ปีที่แล้ว +1

    Please next tutorial of image api

  • @thomasanderson1416
    @thomasanderson1416 ปีที่แล้ว +1

    it’s all good but all the guards and try statements they’re daunting.

  • @drnhnc5034
    @drnhnc5034 หลายเดือนก่อน +1

    BOOM LOOK AT THAT HUH :D:D

  • @user-fu1tt8hf5c
    @user-fu1tt8hf5c 10 หลายเดือนก่อน +1

    the api is now going to a web page that requires a gmail sign up just letting you

    • @user-fu1tt8hf5c
      @user-fu1tt8hf5c 10 หลายเดือนก่อน +1

      also sending to other pages not sure what's going on there

    • @morelogankoshenka
      @morelogankoshenka  10 หลายเดือนก่อน

      mannn I picked the wrong API when making this video!!

  • @ARKillsZombies1
    @ARKillsZombies1 ปีที่แล้ว

    great video sadly I think this api is offline now :/

    • @morelogankoshenka
      @morelogankoshenka  ปีที่แล้ว

      ahhh dang! I’ll have to look into it.

    • @nickpavlov4012
      @nickpavlov4012 ปีที่แล้ว +1

      ya doesnt work (((

    • @nickpavlov4012
      @nickpavlov4012 ปีที่แล้ว

      @@morelogankoshenka lol why did you delete my comment?

    • @morelogankoshenka
      @morelogankoshenka  ปีที่แล้ว

      @@nickpavlov4012 it’s still there bub, I don’t delete any comments

    • @morelogankoshenka
      @morelogankoshenka  ปีที่แล้ว +1

      I wish this API was still active but the same concepts apply to whatever APIs you find / use! 💪🏼

  • @jovanyjimenez5808
    @jovanyjimenez5808 ปีที่แล้ว +1

    Great attitude, but you need to explain what you are using and why. learning is not about getting to the destination without knowing how we got there.