Parsing JSON in Swift: The Complete Guide [With Examples]
Matteo Manferdini Blog
by Matteo Manferdini
3w ago
Parsing JSON data is fundamental to any iOS app that performs remote REST API calls. Thanks to the Codable protocols introduced in Swift 4, Swift has a native and idiomatic way to parse JSON data. Paired with the JSONDecoder class, the Decodable protocol allows straightforward JSON decoding in a few lines of code and more sophisticated techniques to handle all the possible data formats and edge cases. Architecting SwiftUI apps with MVC and MVVM GET THE FREE BOOK NOW Table of contents How to parse JSON data in Swift Advanced techniques to parse complex JSON data Building a SwiftUI app that ..read more
Visit website
Decoding JSON with Dynamic Types in Swift [With Codable]
Matteo Manferdini Blog
by Matteo Manferdini
1M ago
JSON data is not always homogenous. There are instances in which a JSON object field can contain objects containing different information. In such cases, the Swift type for the decoding must be determined dynamically. Architecting SwiftUI apps with MVC and MVVM GET THE FREE BOOK NOW Table of contents Dynamic JSON objects usually carry their type information in a field Making concrete Swift types conform to the same protocol Decoding a JSON object according to its dynamic type with a custom decoding initializer Dynamic JSON objects usually carry their type information in a field Working w ..read more
Visit website
Decoding and Flattening Nested JSON with Codable
Matteo Manferdini Blog
by Matteo Manferdini
1M ago
JSON data is rarely flat and often contains nested objects. Nested JSON data can be decoded in Swift using the Decodable protocol and the JSONDecoder class.  However, how you decode nested JSON objects depends on the final result you want to obtain. Architecting SwiftUI apps with MVC and MVVM GET THE FREE BOOK NOW Table of contents Decoding nested JSON objects using nested Swift types Flattening single JSON objects using a nested keyed container Flattening arrays of JSON objects using a nested unkeyed container Decoding nested JSON objects using nested Swift types The easiest way to ..read more
Visit website
Coding Keys in Swift with Decodable: How and When to Use Them
Matteo Manferdini Blog
by Matteo Manferdini
1M ago
You can easily parse JSON data in Swift by creating a Decodable type that matches the structure of a JSON object. However, we often need to decode JSON data that does not match the model types of our apps. In those cases, we need to control the decoding using coding keys. Architecting SwiftUI apps with MVC and MVVM GET THE FREE BOOK NOW Table of contents How CodingKeys work and when you need them Renaming the fields of a JSON object to follow Swift’s conventions Selecting the stored properties of a decodable Swift type that must be decoded Adding extra logic to the decoding process through ..read more
Visit website
Parse JSON from an API URL in Swift [Codable and URLSession]
Matteo Manferdini Blog
by Matteo Manferdini
1M ago
Fetching and parsing the JSON data returned by a URL is straightforward in Swift, thanks to the JSONDecoder and URLSession classes of the Foundation framework. Architecting SwiftUI apps with MVC and MVVM GET THE FREE BOOK NOW Table of contents How to fetch JSON data from an API URL in Swift Fetching the JSON data using a Task in SwiftUI How to fetch JSON data from an API URL in Swift To fetch and parse JSON data from an API URL in Swift, you need to: Identify the URL of the resource you want to download; Create Decodable Swift types reflecting the JSON you want to parse; Download the da ..read more
Visit website
Parse JSON in Swift without Codable [Arrays and Dictionaries]
Matteo Manferdini Blog
by Matteo Manferdini
1M ago
Since Swift 4, the recommended way to parse JSON data is to use the Codable protocols with the JSONEncoder and JSONDecoder classes. However, this method requires creating Swift model types that match the JSON data structure you need to decode. Sometimes, you might want to avoid creating extra Swift types if you only need to read some of the information in the JSON data locally. Architecting SwiftUI apps with MVC and MVVM GET THE FREE BOOK NOW The Foundation framework contains an old class called JSONSerialization, which was the standard way to decode JSON data before the Codable protocols we ..read more
Visit website
Reading a JSON File in Swift [Bundle and Documents Directory]
Matteo Manferdini Blog
by Matteo Manferdini
1M ago
Apps often need to read the content of JSON files for different reasons. You can save static JSON data inside an Xcode project to populate your app or use it in SwiftUI previews. Apps can also save data inside the device’s documents directory. Architecting SwiftUI apps with MVC and MVVM GET THE FREE BOOK NOW Table of contents Getting sample JSON data to model your Swift types How to encode and decode the contents of a JSON file in Swift Reading a JSON file from the app’s main bundle Reading a JSON file from the app’s document directory Getting sample JSON data to model your Swift types M ..read more
Visit website
Swift Data to JSON String: Formatting and Pretty Printing
Matteo Manferdini Blog
by Matteo Manferdini
1M ago
JSON data is often not human-readable because web servers strip all white space to save bandwidth. Converting JSON data into a formatted string helps build decodable Swift types and is useful when debugging and testing an app’s code. Architecting SwiftUI apps with MVC and MVVM GET THE FREE BOOK NOW If all you need to do is format the JSON data you got from a REST API, you can copy and paste it inside an online JSON formatter. However, you often need to pretty-print the JSON data inside your app. In that case, you must first convert it into a Swift value and revert it into formatted JSON data ..read more
Visit website
Date decoding strategies in Swift [with Examples]
Matteo Manferdini Blog
by Matteo Manferdini
1M ago
The Date type is a Codable type in Swift. However, dates are not straightforward to decode, like other types conforming to the Codable protocol, because the default format used by JSONDecoder is not standard in JSON. Dates can be encoded using different formats in JSON data. To handle all these formats, the JSONDecoder class has a  dateDecodingStrategy property, which can be configured with several decoding strategies according to the date format you must decode. Architecting SwiftUI apps with MVC and MVVM GET THE FREE BOOK NOW Table of contents Decoding dates in the ISO 8601 format ..read more
Visit website
How to Become an iOS Developer [2023 Salary and Skills]
Matteo Manferdini Blog
by Matteo Manferdini
6M ago
Becoming an iOS developer is a great career choice that allows you to demand a high salary and work on interesting projects at top companies. Learning iOS development can take time, but you can speed up the process and optimize your learning following the steps I outline in this article. Architecting SwiftUI apps with MVC and MVVM GET THE FREE BOOK NOW Table of contents What is an iOS Developer? The Technical Skills Roadmap for beginner iOS developers Going Past the Entry Level Jobs The iOS Developer Career Path Devices and Technical Requirements Chapter 1 What is an iOS Developer? An iO ..read more
Visit website

Follow Matteo Manferdini Blog on FeedSpot

Continue with Google
Continue with Apple
OR