Bird
0
0

Which of the following is the correct way to declare a Swift struct that can be encoded and decoded using Codable?

easy📝 Syntax Q12 of 15
iOS Swift - Networking
Which of the following is the correct way to declare a Swift struct that can be encoded and decoded using Codable?
Astruct User { var name: String }
Bstruct User: Decodable { var name: String }
Cclass User: Codable { var name: String }
Dstruct User: Codable { var name: String }
Step-by-Step Solution
Solution:
  1. Step 1: Check the struct declaration

    To use Codable, the struct must conform to the Codable protocol explicitly.
  2. 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.
  3. Final Answer:

    struct User: Codable { var name: String } -> Option D
  4. Quick Check:

    Struct + Codable = struct User: Codable { var name: String } [OK]
Quick Trick: Use struct with Codable for JSON parsing [OK]
Common Mistakes:
  • Forgetting to add Codable conformance
  • Using class instead of struct when asked
  • Using only Decodable without Encodable

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes