iT邦幫忙

2023 iThome 鐵人賽

DAY 19
0
Mobile Development

SwiftUI 男孩系列 第 19

Day 19: Codable, Equatable, Identifiable

  • 分享至 

  • xImage
  •  

https://ithelp.ithome.com.tw/upload/images/20231003/20130138G51eo9MYY9.jpg
Photo by Michael Amadeus on Unsplash
Queenstown, New Zealand

假如今天我有一份 JSON Payload 如下:


[{"id":"ae70743c-6053-11ee-8c99-0242ac120002","name":"Auckland","items":[{"id":"654ab5fa-6054-11ee-8c99-0242ac120002","name":"Glen Innes Pool","photoCredit":"GlenInnesPool","description":"A hub for your family, Glen Innes Pool and Leisure Centre gets you active with multiple pool, fitness and leisure options. Join a group fitness class, learn to swim, have a splash in the water or book your kids into the onsite crèche – the choice is yours!"},{"id":"654ab910-6054-11ee-8c99-0242ac120002","name":"Lloyd Elsmore Park Pool","photoCredit":"LloydElsmoreParkPool","description":"Lloyd Elsmore Park Pool and Leisure Centre is your local Pakuranga spot for all things active — jump in!"},{"id":"654aba64-6054-11ee-8c99-0242ac120002","name":"Manurewa Pool","photoCredit":"ManurewaPoolAndLeisureCentre","description":"Situated in Mountfort Park, this large busy centre has a range of services to keep you and your whanau active an entertained. Facilities include indoor swimming pools, fitness centre, café, and spaces available for hire for your next event.\nTake the plunge and discover a new watersport. The Manurewa Pool and Leisure Centre is home to Manukau’s first deep water pool. In addition to the 25-metre six lane lap pool, the facility includes a 25-metre graduated leisure pool, teaching pool, and toddlers’ pool.\nThere’s even a spa and sauna, ideal for relaxing tired muscles."},{"id":"654ac018-6054-11ee-8c99-0242ac120002","name":"Mt Albert Aquatic Centre","photoCredit":"MtAlbertAquaticCentre","description":"Mt Albert Aquatic Centre is a local community pool that offers you a heated indoor 25m competition pool, as well as a wave pool, lazy river, spa, sauna, swimming lessons and aqua aerobics."},{"id":"654ac16c-6054-11ee-8c99-0242ac120002","name":"TepidBaths","photoCredit":"TepidBaths","description":"‘The Teps’ is an iconic part of Auckland’s history. Built over 100 years ago and refurbished in 2012, our centre has two pools, spa, steam and sauna facilities and a state-of-the-art fitness centre to help you get active. Whether your goal is general fitness, weight loss, sports training or rehabilitation, the Tepid Baths is here to help you get active your way."},{"id":"654ac310-6054-11ee-8c99-0242ac120002","name":"West Wave Pool","photoCredit":"WestWavePool","description":"West Wave is located right in the heart of Henderson. We have something for everyone, from a hydroslide, diving pool and wave pool through to a steam room, sauna, hydrotherapy pool and 50m lap pool."}]}]

Oh my gosh ! !

這可讀性不是給人看的。用 json formatter 整理一下 !!

https://ithelp.ithome.com.tw/upload/images/20231003/20130138viVIV5Wweh.png

https://ithelp.ithome.com.tw/upload/images/20231003/20130138iFxRiy3D6s.jpg

舒暢!!

我目前用 tab bar 定義專案檔案結構 (這很見仁見智)

https://ithelp.ithome.com.tw/upload/images/20231003/20130138sfRkXKb6mU.png

我在 Maps 的 Model 資料夾下建立 pools.json and Pools.swift

https://ithelp.ithome.com.tw/upload/images/20231003/20130138LkiYOHpCnd.png


JSON 數據解析成 Swift 物件結構

通常手 key 建立 JSON 數據定義模型,非常耗費青春,又容易出錯,少打 a , e , s 字母。

這時候用 quicktype 幫我把API JSON 資料轉換成 Swift 物件

https://ithelp.ithome.com.tw/upload/images/20231003/20130138PmisjIT78M.png

再稍微修改程式碼如下

struct PoolsSection: Codable, Identifiable {
    var id: UUID
    var name: String
    var items: [PoolItem]
}

struct PoolItem: Codable, Equatable, Identifiable {
    var id: UUID
    var name: String
    var photoCredit: String
    var description: String
}

Tips: Press “option + 滑鼠右鍵” 選取 key word 會跳出提示,可以點選 “Open in Developer Documentation” 倒去第二張圖的Documentation。
https://ithelp.ithome.com.tw/upload/images/20231003/20130138n2Z7uev1Gv.png

可以看支援版本從第幾版開始,通常版本向下兼容2代,Example : iOS 17 - 2 = iOS 15,還是要評估看 firebase log 看 User 還有多少人iOS OS 一直不升級,要不要放生或 push notification 推播:該買新手機囉,未來即將不支援囉~😇
要不然維護舊版本,這真的很考驗 iOS Developer 功力 🫨
https://ithelp.ithome.com.tw/upload/images/20231003/20130138JH2YWMAU23.png
Tips: 良好習慣,要看文件。咒術刻在腦海裡。最好大聲唸出來!
https://ithelp.ithome.com.tw/upload/images/20231003/20130138KtKxHSJk6z.png

Mock API

下集待續


上一篇
Day 18: ~~不義遊戲~~跳轉頁面,逆轉!
下一篇
Day 20 : Mock API
系列文
SwiftUI 男孩30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言