Encoding ve Decoding
JSON gibi yapıların verileri encoding ve decoding hale getirilebilir.Swift standart kütüphanesi, veri kodlama ve kod çözme için standartlaştırılmış bir yaklaşım tanımlamaktadır. Bunlar, özel türlerde Encodable ve Decodable protokolleri uygulanarak sağlanabilir. Bu şekilde JSON verileri okuyabiliriz ya da kendi verilerimizi JSON formatına çevirebiliriz. Hem kodlamayı hemde kod çözmeyi sağlayan yapımız da Codable olmaktadır.
Konuyla ilgili apple dökümanlarını incelemek isterseniz tıklayınız.
JSON verilerinin daha kolay kullanılması için encodable ve decodable protokolleri vardır ve bunların her ikisini temsil eden Codable typealias vardır;
typealias Codable = Decodable & Encodable
Codable ile hem decodable hem de encodable işlemlerini yapabiliriz.
CodingKeys = CodingKeys, JSON anahtarlarınızı temsil etmek için belirli değişken adlarını kullanmanıza izin verir.
Örnek;
JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"job": { | |
"title": "iOS Developer", | |
"salary": 5000 | |
}, | |
"firstName": "Atakan", | |
"lastName": "KURT", | |
"age": 30 | |
} |
Decodable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
struct Job: Decodable { | |
var title: String | |
var salary: Float | |
init(title: String, salary: Float) { | |
self.title = title | |
self.salary = salary | |
} | |
enum CodingKeys: String, CodingKey { | |
case title, salary | |
} | |
} | |
struct Person: Decodable { | |
var job: Job | |
var firstName: String | |
var lastName: String | |
var age: Int | |
init(job: Job, firstName: String, lastName: String, age: Int) { | |
self.job = job | |
self.firstName = firstName | |
self.lastName = lastName | |
self.age = age | |
} | |
enum CodingKeys: String, CodingKey { | |
case job = "job", firstName = "firstName", lastName = "lastName", age = "age" | |
} | |
} | |
let rawData = """ | |
{ | |
"job": { | |
"title": "iOS Developer", | |
"salary": 5000 | |
}, | |
"firstName": "Atakan", | |
"lastName": "KURT", | |
"age": 30 | |
} | |
""".data(using: .utf8)! | |
let person = try JSONDecoder().decode(Person.self, from: rawData) | |
print(person.firstName) // Atakan | |
print(person.lastName) // KURT | |
print(person.job.title) // iOS Developer |
Encodable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
struct Person: Encodable { | |
var firstname: String | |
var lastname: String | |
var age: Int | |
init(firstname: String, lastname: String, age: Int) { | |
self.firstname = firstname | |
self.lastname = lastname | |
self.age = age | |
} | |
} | |
struct People: Encodable { | |
var members: [Person] | |
init(members: [Person]) { | |
self.members = members | |
} | |
} | |
let people = People(members: [Person(firstName: "Atakan", lastname: "KURT", age: 30), Person(firstname: "Erkan", lastname: "KURT", age: 25)]) | |
let encoded = try JSONEncoder().encode(people) | |
//String(data: encoded, encoding: .utf8) | |
--------> //{"members":[{"firstname":"Atakan","lastname":"KURT","age":30},{"firstname":"Erkan","lastname":"KURT","age":25}]} |
Hiç yorum yok:
Yorum Gönder
Yorumunuz için teşekkür ederim.