Content

Data that is passed around in Vapor is often derived from the Content type. It's a class that conforms to Codable, which means that it can convert from and into JSON, XML, or any other data structure without any issues. Take a look at the following struct:

struct SomeData: Content {
var stringValue: String
var optionalValue: String?
var anotherValue: Int
var anotherContent: ClassThatIsContent
}

In JSON, the data could look like this:

{
"stringValue": "a random string",
"anotherValue": 10,
"anotherContent": { ... }
}

We can use any object or class that is implementing the Codable protocol. All the standard types (String, Int, and Bool) are conformed by default. But we can also implement our own types. This allows us to represent even complex JSON structures easily by building all the needed structs (or classes).

Note that Swift is a statically typed language. If you have dealt with dynamically typed languages before, be aware that Swift is not tolerating implicit conversions from String to Int and vice versa. To convert them, you need to use a function that is provided.

Content is a helpful protocol that allows you to write structs or classes for your inputs and outputs.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.55.151