What is the purpose of the Codable protocol in Swift?
The Codable protocol allows Swift types to be easily encoded to and decoded from external representations like JSON, making data parsing and serialization simple.
How do you decode JSON data into a Swift struct that conforms to Codable?
Use <code>JSONDecoder</code> to decode JSON data into the struct. For example: <br><code>let decoded = try JSONDecoder().decode(MyStruct.self, from: jsonData)</code>
Click to reveal answer
beginner
What must you do to a Swift struct to make it parse JSON automatically?
Make the struct conform to Codable and ensure all its properties are also Codable types.
Click to reveal answer
intermediate
How can you handle JSON keys that don't match your Swift property names?
Implement a CodingKeys enum inside your struct to map JSON keys to your property names.
Click to reveal answer
What does the Codable protocol help you do in Swift?
AHandle network connections
BCreate user interfaces
CManage memory manually
DParse and generate JSON data easily
✗ Incorrect
Codable is designed to help encode and decode data formats like JSON, not UI or networking.
Which class is used to decode JSON data into Swift types?
AJSONEncoder
BJSONDecoder
CDataDecoder
DJSONParser
✗ Incorrect
JSONDecoder decodes JSON data into Swift types conforming to Decodable.
If your JSON key is user_name but your Swift property is userName, how do you handle this mismatch?
AUse a <code>CodingKeys</code> enum to map keys
BRename the property to <code>user_name</code>
CChange the JSON key to <code>userName</code>
DYou cannot handle this mismatch
✗ Incorrect
The CodingKeys enum lets you map JSON keys to different property names.
What must all properties of a Codable struct be?
AAlso conform to <code>Codable</code>
BPrivate
CStrings only
DOptional
✗ Incorrect
All properties must be Codable to be encoded or decoded automatically.
Which protocol is NOT part of Codable?
AEncodable
BDecodable
CSerializable
DNone of the above
✗ Incorrect
Serializable is not a Swift protocol; Codable combines Encodable and Decodable.
Explain how to use the Codable protocol to parse JSON data into a Swift struct.
Think about the steps from defining the struct to decoding JSON.
You got /4 concepts.
Describe how you would handle a JSON key that does not match your Swift property name when using Codable.
Focus on the enum inside the struct.
You got /3 concepts.
Practice
(1/5)
1. What does the Codable protocol in Swift help you do?
easy
A. Convert Swift objects to and from JSON easily
B. Create user interface elements
C. Manage app navigation
D. Handle network connections
Solution
Step 1: Understand the purpose of Codable
The Codable protocol is designed to simplify converting data between Swift objects and external formats like JSON.
Step 2: Identify the correct use case
It is not related to UI, navigation, or networking directly, but specifically for encoding and decoding data.
Final Answer:
Convert Swift objects to and from JSON easily -> Option A
Quick Check:
Codable = Convert JSON [OK]
Hint: Codable means easy JSON conversion [OK]
Common Mistakes:
Confusing Codable with UI or navigation tasks
Thinking Codable manages network calls
Assuming Codable is for database storage
2. Which of the following is the correct way to declare a Swift struct that can be encoded and decoded using Codable?
easy
A. struct User { var name: String }
B. struct User: Decodable { var name: String }
C. class User: Codable { var name: String }
D. struct User: Codable { var name: String }
Solution
Step 1: Check the struct declaration
To use Codable, the struct must conform to the Codable protocol explicitly.
Step 2: Verify the syntax
struct User: Codable { var name: String } correctly declares a struct conforming to Codable. struct User { var name: String } lacks Codable conformance. class User: Codable { var name: String } uses a class, which is valid but not asked here. struct User: Decodable { var name: String } only conforms to Decodable, missing Encodable.
Final Answer:
struct User: Codable { var name: String } -> Option D