SlideShare a Scribd company logo
typealias Codable = Decodable & Encodable
protocol Decodable
protocol Encodable


















let fileURL: URL = URL(fileURLWithPath: "file.plist")
guard let plistDic: [String:Any] =

NSDictionary(contentsOf: fileURL) as? [String:Any]
else { return }
if let age: Int = plistDic["age"] as? Int {
print(age)
}
if let name: String = plistDic["name"] as? String {
print(name)
}
let fileURL: URL = URL(fileURLWithPath: "file.plist")
let data: Data = try! Data(contentsOf: fileURL)
let plistDecoder: PropertyListDecoder =

PropertyListDecoder()
let friend: Friend = try!

plistDecoder.decode(Friend.self, from: data)
print(friend.name)
print(friend.age)
let fileURL: URL = URL(fileURLWithPath: "file.json")
let data: Data = try! Data(contentsOf: fileURL)
guard let jsonObject: Any = try?

JSONSerialization.jsonObject(with: data, 

options: .allowFragments) else { return }
guard let jsonDic: [String:Any] = jsonObject as? 

[String:Any] else { return }
if let age: Int = jsonDic["age"] as? Int {
print(age)
}
if let name: String = jsonDic["name"] as? String {
print(name)
}
let fileURL: URL = URL(fileURLWithPath: "file.json")
let data: Data = try! Data(contentsOf: fileURL)
let jsonDecoder: JSONDecoder = JSONDecoder()
let friend: Friend = try! 

jsonDecoder.decode(Friend.self, from: data)
print(friend.name)
print(friend.age)
public protocol Encodable {
public func encode(to encoder: Encoder) throws
}
public protocol Decodable {
public init(from decoder: Decoder) throws
}
struct Friend: Codable {
let name: String
let age: Int
let job: String
let isSexy: Bool?
struct Address: Codable {
let country: String
let city: String
}
let address: Address
}


Swift 4 : Codable
Swift 4 : Codable

More Related Content

What's hot

ZIP, GZIP Streams in java
ZIP, GZIP Streams in javaZIP, GZIP Streams in java
ZIP, GZIP Streams in java
Alexey Bovanenko
 
Web Programming
Web ProgrammingWeb Programming
Web Programming
Henry Osborne
 
JSOP in 60 seconds
JSOP in 60 secondsJSOP in 60 seconds
JSOP in 60 seconds
David Nuescheler
 
RESTing with the new Yandex.Disk API, Clemens Аuer
RESTing with the new Yandex.Disk API, Clemens АuerRESTing with the new Yandex.Disk API, Clemens Аuer
RESTing with the new Yandex.Disk API, Clemens Аuer
Yandex
 
Swift Study #7
Swift Study #7Swift Study #7
Swift Study #7
chanju Jeon
 
Java Development with MongoDB (James Williams)
Java Development with MongoDB (James Williams)Java Development with MongoDB (James Williams)
Java Development with MongoDB (James Williams)
MongoSF
 
HaskellとDebianの辛くて甘い関係
HaskellとDebianの辛くて甘い関係HaskellとDebianの辛くて甘い関係
HaskellとDebianの辛くて甘い関係
Kiwamu Okabe
 
Php File Operations
Php File OperationsPhp File Operations
Php File Operations
Jamshid Hashimi
 
Couchdb
CouchdbCouchdb
Using MongoDB With Groovy
Using MongoDB With GroovyUsing MongoDB With Groovy
Using MongoDB With Groovy
James Williams
 
Presentation about graph database
Presentation about graph databasePresentation about graph database
Presentation about graph database
zubair barkat Barkat Ali
 
New text document
New text documentNew text document
New text document
Pavel111212
 

What's hot (12)

ZIP, GZIP Streams in java
ZIP, GZIP Streams in javaZIP, GZIP Streams in java
ZIP, GZIP Streams in java
 
Web Programming
Web ProgrammingWeb Programming
Web Programming
 
JSOP in 60 seconds
JSOP in 60 secondsJSOP in 60 seconds
JSOP in 60 seconds
 
RESTing with the new Yandex.Disk API, Clemens Аuer
RESTing with the new Yandex.Disk API, Clemens АuerRESTing with the new Yandex.Disk API, Clemens Аuer
RESTing with the new Yandex.Disk API, Clemens Аuer
 
Swift Study #7
Swift Study #7Swift Study #7
Swift Study #7
 
Java Development with MongoDB (James Williams)
Java Development with MongoDB (James Williams)Java Development with MongoDB (James Williams)
Java Development with MongoDB (James Williams)
 
HaskellとDebianの辛くて甘い関係
HaskellとDebianの辛くて甘い関係HaskellとDebianの辛くて甘い関係
HaskellとDebianの辛くて甘い関係
 
Php File Operations
Php File OperationsPhp File Operations
Php File Operations
 
Couchdb
CouchdbCouchdb
Couchdb
 
Using MongoDB With Groovy
Using MongoDB With GroovyUsing MongoDB With Groovy
Using MongoDB With Groovy
 
Presentation about graph database
Presentation about graph databasePresentation about graph database
Presentation about graph database
 
New text document
New text documentNew text document
New text document
 

Similar to Swift 4 : Codable

What's New in Swift 4
What's New in Swift 4What's New in Swift 4
What's New in Swift 4
Young Hoo Kim
 
FlashAir Android App Development
FlashAir Android App DevelopmentFlashAir Android App Development
FlashAir Android App Development
FlashAir Developers
 
File in cpp 2016
File in cpp 2016 File in cpp 2016
File in cpp 2016
Dr .Ahmed Tawwab
 
F# delight
F# delightF# delight
F# delight
priort
 
File handling in c language
File handling in c languageFile handling in c language
File handling in c language
Harish Gyanani
 
Saving Data
Saving DataSaving Data
Saving Data
SV.CO
 
File handling-c
File handling-cFile handling-c
Write a program that asks the user for the name of a file. The progra.docx
 Write a program that asks the user for the name of a file. The progra.docx Write a program that asks the user for the name of a file. The progra.docx
Write a program that asks the user for the name of a file. The progra.docx
ajoy21
 
Writing Swift code with great testability
Writing Swift code with great testabilityWriting Swift code with great testability
Writing Swift code with great testability
John Sundell
 
Python build your security tools.pdf
Python build your security tools.pdfPython build your security tools.pdf
Python build your security tools.pdf
TECHNOLOGY CONTROL CO.
 
awesome groovy
awesome groovyawesome groovy
awesome groovy
Paul King
 
#include iostream #include cstring #include vector #i.pdf
 #include iostream #include cstring #include vector #i.pdf #include iostream #include cstring #include vector #i.pdf
#include iostream #include cstring #include vector #i.pdf
anandatalapatra
 
Unit 8
Unit 8Unit 8
Unit 8
siddr
 
working with files
working with filesworking with files
working with files
SangeethaSasi1
 
C++17 std::filesystem - Overview
C++17 std::filesystem - OverviewC++17 std::filesystem - Overview
C++17 std::filesystem - Overview
Bartlomiej Filipek
 
File management
File managementFile management
File management
Mohammed Sikander
 
2019-01-29 - Demystifying Kotlin Coroutines
2019-01-29 - Demystifying Kotlin Coroutines2019-01-29 - Demystifying Kotlin Coroutines
2019-01-29 - Demystifying Kotlin Coroutines
Eamonn Boyle
 
Unix / Linux Command Reference
Unix / Linux Command ReferenceUnix / Linux Command Reference
Unix / Linux Command Reference
Sumankumar Panchal
 
Exmaples of file handling
Exmaples of file handlingExmaples of file handling
Exmaples of file handling
sparkishpearl
 
File handling
File handlingFile handling
File handling
Ans Ali
 

Similar to Swift 4 : Codable (20)

What's New in Swift 4
What's New in Swift 4What's New in Swift 4
What's New in Swift 4
 
FlashAir Android App Development
FlashAir Android App DevelopmentFlashAir Android App Development
FlashAir Android App Development
 
File in cpp 2016
File in cpp 2016 File in cpp 2016
File in cpp 2016
 
F# delight
F# delightF# delight
F# delight
 
File handling in c language
File handling in c languageFile handling in c language
File handling in c language
 
Saving Data
Saving DataSaving Data
Saving Data
 
File handling-c
File handling-cFile handling-c
File handling-c
 
Write a program that asks the user for the name of a file. The progra.docx
 Write a program that asks the user for the name of a file. The progra.docx Write a program that asks the user for the name of a file. The progra.docx
Write a program that asks the user for the name of a file. The progra.docx
 
Writing Swift code with great testability
Writing Swift code with great testabilityWriting Swift code with great testability
Writing Swift code with great testability
 
Python build your security tools.pdf
Python build your security tools.pdfPython build your security tools.pdf
Python build your security tools.pdf
 
awesome groovy
awesome groovyawesome groovy
awesome groovy
 
#include iostream #include cstring #include vector #i.pdf
 #include iostream #include cstring #include vector #i.pdf #include iostream #include cstring #include vector #i.pdf
#include iostream #include cstring #include vector #i.pdf
 
Unit 8
Unit 8Unit 8
Unit 8
 
working with files
working with filesworking with files
working with files
 
C++17 std::filesystem - Overview
C++17 std::filesystem - OverviewC++17 std::filesystem - Overview
C++17 std::filesystem - Overview
 
File management
File managementFile management
File management
 
2019-01-29 - Demystifying Kotlin Coroutines
2019-01-29 - Demystifying Kotlin Coroutines2019-01-29 - Demystifying Kotlin Coroutines
2019-01-29 - Demystifying Kotlin Coroutines
 
Unix / Linux Command Reference
Unix / Linux Command ReferenceUnix / Linux Command Reference
Unix / Linux Command Reference
 
Exmaples of file handling
Exmaples of file handlingExmaples of file handling
Exmaples of file handling
 
File handling
File handlingFile handling
File handling
 

Recently uploaded

A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
kalichargn70th171
 
Trailhead Talks_ Journey of an All-Star Ranger .pptx
Trailhead Talks_ Journey of an All-Star Ranger .pptxTrailhead Talks_ Journey of an All-Star Ranger .pptx
Trailhead Talks_ Journey of an All-Star Ranger .pptx
ImtiazBinMohiuddin
 
美洲杯赔率投注网【​网址​🎉3977·EE​🎉】
美洲杯赔率投注网【​网址​🎉3977·EE​🎉】美洲杯赔率投注网【​网址​🎉3977·EE​🎉】
美洲杯赔率投注网【​网址​🎉3977·EE​🎉】
widenerjobeyrl638
 
Ensuring Efficiency and Speed with Practical Solutions for Clinical Operations
Ensuring Efficiency and Speed with Practical Solutions for Clinical OperationsEnsuring Efficiency and Speed with Practical Solutions for Clinical Operations
Ensuring Efficiency and Speed with Practical Solutions for Clinical Operations
OnePlan Solutions
 
The Comprehensive Guide to Validating Audio-Visual Performances.pdf
The Comprehensive Guide to Validating Audio-Visual Performances.pdfThe Comprehensive Guide to Validating Audio-Visual Performances.pdf
The Comprehensive Guide to Validating Audio-Visual Performances.pdf
kalichargn70th171
 
TheFutureIsDynamic-BoxLang-CFCamp2024.pdf
TheFutureIsDynamic-BoxLang-CFCamp2024.pdfTheFutureIsDynamic-BoxLang-CFCamp2024.pdf
TheFutureIsDynamic-BoxLang-CFCamp2024.pdf
Ortus Solutions, Corp
 
Hands-on with Apache Druid: Installation & Data Ingestion Steps
Hands-on with Apache Druid: Installation & Data Ingestion StepsHands-on with Apache Druid: Installation & Data Ingestion Steps
Hands-on with Apache Druid: Installation & Data Ingestion Steps
servicesNitor
 
Enhanced Screen Flows UI/UX using SLDS with Tom Kitt
Enhanced Screen Flows UI/UX using SLDS with Tom KittEnhanced Screen Flows UI/UX using SLDS with Tom Kitt
Enhanced Screen Flows UI/UX using SLDS with Tom Kitt
Peter Caitens
 
Secure-by-Design Using Hardware and Software Protection for FDA Compliance
Secure-by-Design Using Hardware and Software Protection for FDA ComplianceSecure-by-Design Using Hardware and Software Protection for FDA Compliance
Secure-by-Design Using Hardware and Software Protection for FDA Compliance
ICS
 
Photoshop Tutorial for Beginners (2024 Edition)
Photoshop Tutorial for Beginners (2024 Edition)Photoshop Tutorial for Beginners (2024 Edition)
Photoshop Tutorial for Beginners (2024 Edition)
alowpalsadig
 
Going AOT: Everything you need to know about GraalVM for Java applications
Going AOT: Everything you need to know about GraalVM for Java applicationsGoing AOT: Everything you need to know about GraalVM for Java applications
Going AOT: Everything you need to know about GraalVM for Java applications
Alina Yurenko
 
Best Practices & Tips for a Successful Odoo ERP Implementation
Best Practices & Tips for a Successful Odoo ERP ImplementationBest Practices & Tips for a Successful Odoo ERP Implementation
Best Practices & Tips for a Successful Odoo ERP Implementation
Envertis Software Solutions
 
Folding Cheat Sheet #5 - fifth in a series
Folding Cheat Sheet #5 - fifth in a seriesFolding Cheat Sheet #5 - fifth in a series
Folding Cheat Sheet #5 - fifth in a series
Philip Schwarz
 
Call Girls Bangalore🔥7023059433🔥Best Profile Escorts in Bangalore Available 24/7
Call Girls Bangalore🔥7023059433🔥Best Profile Escorts in Bangalore Available 24/7Call Girls Bangalore🔥7023059433🔥Best Profile Escorts in Bangalore Available 24/7
Call Girls Bangalore🔥7023059433🔥Best Profile Escorts in Bangalore Available 24/7
manji sharman06
 
What’s New in VictoriaLogs - Q2 2024 Update
What’s New in VictoriaLogs - Q2 2024 UpdateWhat’s New in VictoriaLogs - Q2 2024 Update
What’s New in VictoriaLogs - Q2 2024 Update
VictoriaMetrics
 
What’s new in VictoriaMetrics - Q2 2024 Update
What’s new in VictoriaMetrics - Q2 2024 UpdateWhat’s new in VictoriaMetrics - Q2 2024 Update
What’s new in VictoriaMetrics - Q2 2024 Update
VictoriaMetrics
 
Migration From CH 1.0 to CH 2.0 and Mule 4.6 & Java 17 Upgrade.pptx
Migration From CH 1.0 to CH 2.0 and  Mule 4.6 & Java 17 Upgrade.pptxMigration From CH 1.0 to CH 2.0 and  Mule 4.6 & Java 17 Upgrade.pptx
Migration From CH 1.0 to CH 2.0 and Mule 4.6 & Java 17 Upgrade.pptx
ervikas4
 
Orca: Nocode Graphical Editor for Container Orchestration
Orca: Nocode Graphical Editor for Container OrchestrationOrca: Nocode Graphical Editor for Container Orchestration
Orca: Nocode Graphical Editor for Container Orchestration
Pedro J. Molina
 
Cost-Effective Strategies For iOS App Development
Cost-Effective Strategies For iOS App DevelopmentCost-Effective Strategies For iOS App Development
Cost-Effective Strategies For iOS App Development
Softradix Technologies
 

Recently uploaded (20)

A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
 
Trailhead Talks_ Journey of an All-Star Ranger .pptx
Trailhead Talks_ Journey of an All-Star Ranger .pptxTrailhead Talks_ Journey of an All-Star Ranger .pptx
Trailhead Talks_ Journey of an All-Star Ranger .pptx
 
bgiolcb
bgiolcbbgiolcb
bgiolcb
 
美洲杯赔率投注网【​网址​🎉3977·EE​🎉】
美洲杯赔率投注网【​网址​🎉3977·EE​🎉】美洲杯赔率投注网【​网址​🎉3977·EE​🎉】
美洲杯赔率投注网【​网址​🎉3977·EE​🎉】
 
Ensuring Efficiency and Speed with Practical Solutions for Clinical Operations
Ensuring Efficiency and Speed with Practical Solutions for Clinical OperationsEnsuring Efficiency and Speed with Practical Solutions for Clinical Operations
Ensuring Efficiency and Speed with Practical Solutions for Clinical Operations
 
The Comprehensive Guide to Validating Audio-Visual Performances.pdf
The Comprehensive Guide to Validating Audio-Visual Performances.pdfThe Comprehensive Guide to Validating Audio-Visual Performances.pdf
The Comprehensive Guide to Validating Audio-Visual Performances.pdf
 
TheFutureIsDynamic-BoxLang-CFCamp2024.pdf
TheFutureIsDynamic-BoxLang-CFCamp2024.pdfTheFutureIsDynamic-BoxLang-CFCamp2024.pdf
TheFutureIsDynamic-BoxLang-CFCamp2024.pdf
 
Hands-on with Apache Druid: Installation & Data Ingestion Steps
Hands-on with Apache Druid: Installation & Data Ingestion StepsHands-on with Apache Druid: Installation & Data Ingestion Steps
Hands-on with Apache Druid: Installation & Data Ingestion Steps
 
Enhanced Screen Flows UI/UX using SLDS with Tom Kitt
Enhanced Screen Flows UI/UX using SLDS with Tom KittEnhanced Screen Flows UI/UX using SLDS with Tom Kitt
Enhanced Screen Flows UI/UX using SLDS with Tom Kitt
 
Secure-by-Design Using Hardware and Software Protection for FDA Compliance
Secure-by-Design Using Hardware and Software Protection for FDA ComplianceSecure-by-Design Using Hardware and Software Protection for FDA Compliance
Secure-by-Design Using Hardware and Software Protection for FDA Compliance
 
Photoshop Tutorial for Beginners (2024 Edition)
Photoshop Tutorial for Beginners (2024 Edition)Photoshop Tutorial for Beginners (2024 Edition)
Photoshop Tutorial for Beginners (2024 Edition)
 
Going AOT: Everything you need to know about GraalVM for Java applications
Going AOT: Everything you need to know about GraalVM for Java applicationsGoing AOT: Everything you need to know about GraalVM for Java applications
Going AOT: Everything you need to know about GraalVM for Java applications
 
Best Practices & Tips for a Successful Odoo ERP Implementation
Best Practices & Tips for a Successful Odoo ERP ImplementationBest Practices & Tips for a Successful Odoo ERP Implementation
Best Practices & Tips for a Successful Odoo ERP Implementation
 
Folding Cheat Sheet #5 - fifth in a series
Folding Cheat Sheet #5 - fifth in a seriesFolding Cheat Sheet #5 - fifth in a series
Folding Cheat Sheet #5 - fifth in a series
 
Call Girls Bangalore🔥7023059433🔥Best Profile Escorts in Bangalore Available 24/7
Call Girls Bangalore🔥7023059433🔥Best Profile Escorts in Bangalore Available 24/7Call Girls Bangalore🔥7023059433🔥Best Profile Escorts in Bangalore Available 24/7
Call Girls Bangalore🔥7023059433🔥Best Profile Escorts in Bangalore Available 24/7
 
What’s New in VictoriaLogs - Q2 2024 Update
What’s New in VictoriaLogs - Q2 2024 UpdateWhat’s New in VictoriaLogs - Q2 2024 Update
What’s New in VictoriaLogs - Q2 2024 Update
 
What’s new in VictoriaMetrics - Q2 2024 Update
What’s new in VictoriaMetrics - Q2 2024 UpdateWhat’s new in VictoriaMetrics - Q2 2024 Update
What’s new in VictoriaMetrics - Q2 2024 Update
 
Migration From CH 1.0 to CH 2.0 and Mule 4.6 & Java 17 Upgrade.pptx
Migration From CH 1.0 to CH 2.0 and  Mule 4.6 & Java 17 Upgrade.pptxMigration From CH 1.0 to CH 2.0 and  Mule 4.6 & Java 17 Upgrade.pptx
Migration From CH 1.0 to CH 2.0 and Mule 4.6 & Java 17 Upgrade.pptx
 
Orca: Nocode Graphical Editor for Container Orchestration
Orca: Nocode Graphical Editor for Container OrchestrationOrca: Nocode Graphical Editor for Container Orchestration
Orca: Nocode Graphical Editor for Container Orchestration
 
Cost-Effective Strategies For iOS App Development
Cost-Effective Strategies For iOS App DevelopmentCost-Effective Strategies For iOS App Development
Cost-Effective Strategies For iOS App Development
 

Swift 4 : Codable

  • 1.
  • 2. typealias Codable = Decodable & Encodable
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10. let fileURL: URL = URL(fileURLWithPath: "file.plist") guard let plistDic: [String:Any] =
 NSDictionary(contentsOf: fileURL) as? [String:Any] else { return } if let age: Int = plistDic["age"] as? Int { print(age) } if let name: String = plistDic["name"] as? String { print(name) }
  • 11. let fileURL: URL = URL(fileURLWithPath: "file.plist") let data: Data = try! Data(contentsOf: fileURL) let plistDecoder: PropertyListDecoder =
 PropertyListDecoder() let friend: Friend = try!
 plistDecoder.decode(Friend.self, from: data) print(friend.name) print(friend.age)
  • 12.
  • 13. let fileURL: URL = URL(fileURLWithPath: "file.json") let data: Data = try! Data(contentsOf: fileURL) guard let jsonObject: Any = try?
 JSONSerialization.jsonObject(with: data, 
 options: .allowFragments) else { return } guard let jsonDic: [String:Any] = jsonObject as? 
 [String:Any] else { return } if let age: Int = jsonDic["age"] as? Int { print(age) } if let name: String = jsonDic["name"] as? String { print(name) }
  • 14. let fileURL: URL = URL(fileURLWithPath: "file.json") let data: Data = try! Data(contentsOf: fileURL) let jsonDecoder: JSONDecoder = JSONDecoder() let friend: Friend = try! 
 jsonDecoder.decode(Friend.self, from: data) print(friend.name) print(friend.age)
  • 15. public protocol Encodable { public func encode(to encoder: Encoder) throws } public protocol Decodable { public init(from decoder: Decoder) throws }
  • 16. struct Friend: Codable { let name: String let age: Int let job: String let isSexy: Bool? struct Address: Codable { let country: String let city: String } let address: Address }
  • 17.
  • 18.