Introduction of Swift Codable
Apple has launched the new feature in Swift to parse JSON Swift Codable protocol. Swift 4 has introduced standard Codeable protocol which is part of the Foundation framework and JSON parsing became a single or couple of lines of code.
The Codable
is a combination of two protocols, Decodable
and Encodable
. init(from: Decoder)
and encode(to: Encoder)
respectively. In other words, for a type to be “decodable” or “encodable”, they’ll need to “decode from something” and “encode to something”.
The real magic of Codable
happens with the Decoder
and Encoder
protocols. The JSONDecoder
and JSONEncoder
classes use those two protocols to provide the functionality to decode/encode JSON.
Useful Points
1-> Mismatch between the strong data types of Swift and lose data types of JSON has been internally handled by the Swift compiler. We can now handle Swift Data types like Date, URL, Float, etc
2-> Using Codable we can make the app faster. because with hel of codeable we don’t need to worried about Complex JSON and with codeable easily managed by Nesting Structure.
3-> For array JSON you don’t need to use any loop for store the data in an array object.
4-> USing Codeable we can model JSONObject or PropertyList file into equivalent Struct or Classes by writing very few lines of code
Note-> Here we will take a simple example of print the user wallet transaction details with the help of Codeable Protocol.
Step 1-> First make the model.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
struct CustomerwalletDetail: Codable { var customerwalletdetails: Wallet? } struct Wallet: Codable { var transaction: [WalletDetails]? } struct WalletDetails: Codable { var id_order: String? var wallet_amount: String? var wallet_amount_color: String? var transaction_type: String? var transaction_type_color: String? var payment_status_color: String? var date_add: String? } |
Step 2-> Hit the API and get the response.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
Here we are decoding the below type of JSON format Data. { "is_customer_active": "1", "current_wallet_amount": "SAR543.51", "minimum_adding_amount": "0", "pagination": { "totaltransactions": "28", "perpage": "15", "showingtransactions": "15" }, "customerwalletdetails": { "transaction": [ { "id_order": "185", "wallet_amount": "₹619.14", "wallet_amount_color": "#32CD32", "transaction_type": "Pay with Wallet", "transaction_type_color": "#FF69B4", "payment_status": "Payment Accepted", "payment_status_color": "#32CD32", "date_add": "2019-04-25 10:37:32" }, { "id_order": "184", "wallet_amount": "₹651.72", "wallet_amount_color": "#32CD32", "transaction_type": "Pay with Wallet", "transaction_type_color": "#FF69B4", "payment_status": "Payment Accepted", "payment_status_color": "#32CD32", "date_add": "2019-04-25 10:24:46" }, { "id_order": "183", "wallet_amount": "RUB513.08", "wallet_amount_color": "#32CD32", "transaction_type": "Pay with Wallet", "transaction_type_color": "#FF69B4", "payment_status": "Payment Accepted", "payment_status_color": "#32CD32", "date_add": "2019-04-24 15:55:40" } ] } } guard let gitUrl = URL(string: "ApiUrl--->") else { return } URLSession.shared.dataTask(with: gitUrl) { (data, response , error) in guard let data = data else { return } do { let decoder = JSONDecoder() let gitData = try decoder.decode(CustomerwalletDetail.self, from: data) print(gitData.customerwalletdetails ?? (Any).self) } catch let err { print("Err", err) } }.resume() |
Step 3-> After Api’s response we use JSONDecoder to decode the data with our model type.
1 2 3 4 5 6 7 |
do { let decoder = JSONDecoder() let gitData = try decoder.decode(CustomerwalletDetail.self, from: data) print(gitData.customerwalletdetails ?? (Any).self) } catch let err { print("Err", err) } |
->After run the project you will get this output.
->Now you can get an index value with the help of your defined keys.
1 2 |
print(gitData.customerwalletdetails?.transaction[index].wallet_amount ?? "" ?? (Any).self) Output-> ₹619.14" |
Conclusion
So pls follow the above step and And if you have any issue or suggestion you can leave your message in the comment section I will try to solve this.