Parse string field JSON API response to the right data type | Hindi Tutorial

Поделиться
HTML-код
  • Опубликовано: 1 авг 2024
  • Sometimes API developers return JSON response where every field is of the type string and to parse string JSON API response iOS developers create a api response model where every property is declared of the type string and that's a bad approach as it does more harm than good.
    In this video we will explore how we can parse string JSON API response in Swift by setting the right data type
    Given are the content of the video
    0:00 introduction
    0:16 problem statement
    1:38 convincing the api devs
    2:40 coding keys and decodable init function
    3:09 code walkthrough
    4:41 parsing string field json
    5:22 setting the right data type
    6:40 decodable init function
    8:08 manual mapping
    9:55 date manual mapping
    11:09 mapping result
    11:47 use of force unwrapping (crash operator)
    13:49 trade-offs of manual mapping
    14:45 concrete solution
    15:00 subscribe like and share
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ✅ Subscribe to the channel and stay ahead of everyone by following the right way for writing clean code in swift. 🔥🔥🔥🔥🔥
    🔗Click on the link to subscribe: ruclips.net/user/codecat?sub...
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Video links mentioned
    1. Don't define everything as string: • Don't use string every...
    2. Manual decoding: • Decode JSON manually i...
    3. JSON Parsing (Coding keys): • Decodable in swift | J...
    -----------------------------------------------------------------------------------------------------------------------
    📥 Download the source code from
    Github: github.com/codecat15/RUclips-...
    -----------------------------------------------------------------------------------------------------------------------
    ############################################
    Connect with me on social media
    Instagram: codecat15
    📧: codecat15@gmail.com
    🐦: / codecat15
    Facebook: / code.cat.792
    Github: github.com/codecat15
    ###########################################
    #swiftApiTutorial #swiftTutorial #iostutorial

Комментарии • 44

  • @rahuljamba5846
    @rahuljamba5846 Год назад +1

    Very good ravi boss

    • @CodeCat15
      @CodeCat15  Год назад +1

      Glad it was helpful Rahul

  • @ranganathchenna4559
    @ranganathchenna4559 Год назад +1

    good explanation

  • @junaidkamoka4059
    @junaidkamoka4059 Год назад +1

    🔥

  • @rachitsharma8829
    @rachitsharma8829 Год назад +1

    Your all videos are very informative.. Thanks 🎉

    • @CodeCat15
      @CodeCat15  Год назад

      Thanks Rachit, am glad this was helpful

  • @shivanshkumar1134
    @shivanshkumar1134 Год назад +1

    Very much informative Ravi.. Great Tutorial as always

    • @CodeCat15
      @CodeCat15  Год назад

      Thank you I am glad this was helpful Shivansh.

  • @programming5523
    @programming5523 Год назад +1

    your explanations are good i stumbled acroos your channel today is their a possibility that you explain in English

    • @CodeCat15
      @CodeCat15  Год назад

      I have a second channel called the iOS cafe and over there me and other developers post content in English. The best I can do here is to add English subtitles apologies for the inconvenienc.

  • @cwansu
    @cwansu Год назад +1

    Without any exceptions this video is very useful.

    • @CodeCat15
      @CodeCat15  Год назад

      Glad to hear that :) do share the video with your iOS group.

  • @maqsdiadam8524
    @maqsdiadam8524 Год назад +1

    sir need a video on existing projects,
    Being a fresher I got a job , and I have to work on project, so many new things are there like finding particular view and ...

    • @CodeCat15
      @CodeCat15  Год назад

      I did a code review session on MVVM which can help you to begin with, Hope that helps
      ruclips.net/video/7lvUtcMhRjw/видео.html

  • @manojkumardas4164
    @manojkumardas4164 Год назад +1

    Nice and informative video.
    Can you create a video for handling day light saving, regional date, UTC date and what will be the ideal way for handling date inside app Or back end??

    • @CodeCat15
      @CodeCat15  Год назад

      I have a video on that, if the date time you receive from the server has an offset then the code should handle the daylight saving on it's own else it won't
      Video link: ruclips.net/video/U6NHlCBPAF4/видео.html

  • @abanoubashraf1308
    @abanoubashraf1308 Год назад +1

    very useful video, can you plz share your editor theme and the font name plz?

    • @CodeCat15
      @CodeCat15  Год назад

      Its a custom theme I made, accidentaly it turned out to be okay. The font name that I use is source Code pro.

  • @hritiksharma5152
    @hritiksharma5152 Год назад +2

    What if there time also coming along with date but what we need is only date.. is it required to have formatter with same format.. are we supposed to format it once again using other date formatter to get only date

    • @CodeCat15
      @CodeCat15  Год назад

      You can specify the date configurations to the JSONDecoder along with the format that you want the dates to be parsed in,
      I have experienced that in most cases it’s not required but when you get a UTC OR date in some other format and you want to transform that date based on the user region then we can’t avoid using formatter

    • @hritiksharma5152
      @hritiksharma5152 Год назад

      @@CodeCat15 i have one more doubt.. why are we not using multiple cache block with decodingError.typeMismatch cases

  • @ankit76004
    @ankit76004 Год назад +1

    Hi Sir , can you make a video on how can we use the Amazon product api ..

    • @CodeCat15
      @CodeCat15  Год назад

      I have a playlist on API parsing along with implementing good practices for API parsing, you can take the reference from there and try parsing the amazon product api, if you face issues please feel free to email me your issues and I'll check it out
      Playlist link: ruclips.net/p/PLb5R4QC2DtFuXr4177KQ2lIXOkqwq97a4

  • @softtechmishrajiios
    @softtechmishrajiios Год назад +1

    Sir please make a video on fetch token(authentication) through jwt and then get data from Api after authentication

    • @CodeCat15
      @CodeCat15  Год назад +1

      this will be added soon in the API playlist

  • @afzaalyaqub786
    @afzaalyaqub786 Год назад

    Good work

    • @CodeCat15
      @CodeCat15  Год назад

      Thank you, glad it was helpful

  • @bhojakparth1788
    @bhojakparth1788 Год назад +1

    As always, very informative video, just one thing to ask, can't we make every model property to optional, to avoid crash of force unwrapping?

    • @CodeCat15
      @CodeCat15  Год назад

      You can but if required properties are marked as optional then when you try using them then you may not get the desired results.
      In this case, we cannot have properties like id as optional, it’s necessary for that field to have a value

    • @taimoorsuleman4185
      @taimoorsuleman4185 Год назад +1

      Very nice and informative video as always ❤
      Question: How to handle if DataType is not fixed ?
      For example if id is sometimes Int and sometimes String in the response?
      I know the best practice is to fix it on Api side but if api side is not fixed then how we can handle on application side ?

    • @CodeCat15
      @CodeCat15  Год назад

      I prefer using coding keys for this because that’s the spot where such conversions can be handled, the response model should always be of the right datatype as it further helps in carrying out certain operations. In the past i covered one more use case where data was sometimes from api coming as string and sometimes as dictionary and that too was handled using codingkeys, check the video out
      Link:ruclips.net/video/oiDE_fQ8Xvc/видео.html

  • @rachitsharma8829
    @rachitsharma8829 Год назад +1

    1 question here,
    We are getting all details in string and casting it to particular datatype that should not be good approach because casting means we are not getting actual value ... and it is giving me optional value as well while casting right ?
    So what is suggestion here ?

    • @CodeCat15
      @CodeCat15  Год назад

      Thank you for asking the question, in the demo we casted a string with int and date values to their respective data types so why would say that doing this transformation is bad or we are not getting the actual value? For optional value I mentioned in the last few minutes that you can use force unwrapping which is at 11:47 but I also explained the concrete solution at 14:45 that the api developer should themselves send us the results with the correct datatype else we have to do this manual mapping.
      Why this manual mapping is needed is because if everything is string then it consumes extra space in the memory which is not good, a detailed explanation of the drawbacks of doing that is in this video
      Don’t use string everywhere: ruclips.net/video/Q4rJnFx684M/видео.html
      Let me know if you still have doubts.

  • @tajhassan1925
    @tajhassan1925 Год назад +1

    Hello bhaiya, can you please make one video about chatGPT.
    Is chatGPT is going to end developers job

    • @CodeCat15
      @CodeCat15  Год назад

      short answer is no, its more or less going to be another source from where developers would copy paste the code from, perhpas when someone makes an actual robot and programs that robot to write code, create release pipelines and uploads the app to the app store then i would think about such things. chatGPT is cool but its going to be another source from where developers are going to copy paste the code.

  • @rakesh58652
    @rakesh58652 Год назад +1

    Ravi what if I don’t use coding key and make it direct Int(id) ?

    • @CodeCat15
      @CodeCat15  Год назад

      at 6:00 you can see when i set the right datatype to a complete string response our app crashes, i won’t recommend a field like id to be optional as well because we search, read, update, delete using this field so it cannot and should not be optional as well.

  • @abdullahhafeez3159
    @abdullahhafeez3159 Год назад +1

    API is Hitting but it wants token what should i do

    • @CodeCat15
      @CodeCat15  Год назад

      Ask the api developer about it, they’ll let you known if they are using OAuth 2 or something else and will point you out on how to get it.

  • @avijitnagare
    @avijitnagare Год назад +1

    Can we restrict to take screenshot in iOS app?

    • @CodeCat15
      @CodeCat15  Год назад +1

      I think you can checkout this documentation from apple
      developer.apple.com/documentation/uikit/uiscreen/2921651-iscaptured

    • @avijitnagare
      @avijitnagare Год назад +1

      @@CodeCat15 thank you very much