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

Swift 4 : Codable

  • 2.
    typealias Codable =Decodable & Encodable
  • 3.
  • 4.
  • 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)
  • 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 }
  • 18.