قالب وردپرس درنا توس
Home / IOS Development / # 315: Codable Basics 📠 – Little Bites of Cocoa

# 315: Codable Basics 📠 – Little Bites of Cocoa



Subjects

Topics

Swift 4 has brought many improvements. Today we're going to cover one of them, it's the new Codable protocol! Codable ships with Swift 4 as part of the Swift Standard Library and allows us to customize our own types to get the encoding and decoding functionality "free".

Encoding and decoding types have never been easier.

Let's go diving!

Before we begin, we can back up one second and see what we're trying to achieve when it comes to encoding and decoding. [1

9659009] In essence, we want to take an instance of a object or struct and convert (or "code" ) to someone else (usually "machine readable ") format. Later we will be able to convert (or "decode" ) the information in that format back to the object or struct .

Today is the most common format for encoding / decoding JSON. It is supported everywhere and has the benefit of being both human and machine readable.

Therefore, we discuss JSON in this Bite. It is important to note that Codable is not specific to just JSON . It is built to support encoding and decoding in just about all formats we can think of.

Earlier we could have used something like JSONSerialization the class in the Foundation to make our code types into JSON .

This works well, but the encoding input and decoder output were either a dictionary or an Array of them. Then we must manually convert to / from our "real" types.

With Codable we can do a lot better! var name : String
} la ship = Romskip ( name : "Skyhopper )

Now, all we need to do is to convert Our ship to JSON is:

  la    data    =    ] try ?    JSONEncoder  ()    Code  (  Ship ) 

This returns a Data which we can send to a server, save to disk or do all else we need with.

Let's convert it to a string really fast so we can look at it:

  if    la    call    =    String  (  data [19659033]:    data     coding for .      utf8 ) [19659028] 
     [] ) 
} 

This Print:

  {  "Name" :   "Skyhopper" 19659033] } 

Nice! Notice how we did not need to specify the names of the keys in JSON they are derived from the properties of our structure .

For decoding, we can easily go the other way:

  la    ship    =    try ?    JSONDecoder  () .   decode ] .      from :    data ) 

Note how both .no and . decode functions can cast and thus we use sample? keywords to return a null Optional whose encoding or decoding fails. 19659028] : : : : 19659027] Codable {
var first name

: String
var lastname : String
}

struct spacecraft : Codable {[19659031] Var name : String
var pilot : person
}

Nice. Now when we code for a spaceship the system notices that the pilot property value is also Codable and makes it right for us automatically:

  {[19659083] "name :   "first name" :   Luke "   " Skyhopper "    " pilot "    "LastName" :  "Skywalker" }} 

It's all for today the next time we're looking at using [19659005] Codable with more complex types of data like Dates . Merry coding!


Source link