قالب وردپرس درنا توس
Home / IOS Development / Coding and decoding in Swift

Coding and decoding in Swift



A common task for iOS apps is to store data and send it over the network. But before you can do that, you need to convert the data into a suitable format through a process called encoding or serialization .

  Example of data encoded.

You must also convert stored data sent over the network to a suitable format before using it in your app. This reverse process is called decoding or deserialization .

  Example of data decoded.

In this tutorial you will learn all you need to know about coding and decoding in Swift by managing your own toy store. You will examine the following topics along the way:

  • Switching between snake case and camel case formats.
  • Define custom encoding keys.
  • Work with key, unkeyed and nested containers.
  • Managing nested types, dates, subclasses and polymorphic types.

It's pretty much to cover, so it's time to get started! :]

Note : This tutorial assumes that you have basic knowledge of JSON . Check out this cheat sheet if you need a quick overview.

Getting Started

Download the start site using the link Download materials at the top or bottom of the tutorial.

Make sure Project Navigator is visible in Xcode by going to See atorer Navigators ▸ View Project Navigator . Open Nested Types .

Codable and Employ Codable :

  struct Toys: Codable {
...
}
Structured Employee: Codable {
...
}

Codable is not a protocol on its own, but an alias for two other protocols: Encodable and Decodable . As you can guess, the two protocols declare that types can be encoded and decoded from another format.

You do not need to do any more because all stored the properties to both Toy and Employee are encoded. Many basic types in Swift Standard Library and Foundation types (such as String and URL ) can be standardized.

Note : You can encode code types into different formats like Property Lists (PLists) XML or JSON but for this tutorial You just want to work with JSON.

Add a JSONEncoder and a JSONDecoder to handle JSON encoding and decoding toys and employees:

  let encoder = JSONEncoder ()
la decoder = JSOND decoder ()

That's all you need to work with JSON. Time for the first coding and decoding challenge!

Coding and Decoding Nested Types

Employee contains a Toy property - it is a shrouded type . The JSON structure of your coded employee matches Employee struct:

  {
"name": "John Appleseed",
"id": 7,
"favoriteToy": {
"name": "Teddy Bear"
}
}
  public structure Employed: Codable {
was name: String
was id: Int
was favorite Clothing: Toy
}

JSON nests name inside favoriteToy and all JSON keys are the same as the stored properties Employee and Toy You can easily understand the JSON structure based on the data types hierarchy. If your property name matches your JSON field names and your properties are all Codable you can easily convert to or from JSON. You will try it now.

The gift department gives employees their favorite cakes as birthday presents. Add the following code to send the employee's data to the Gifts department:

  // 1
la data = try encoder.encode (employee)
// 2
let string = String (data: data, coding: .utf8)!

How this code works:

  1. Code employed to JSON with code (_ :) (I told it was easy!).
  2. Make a string


    Source link