Share
Embed
Codable用のEncoder/Decoderを作ってみた
Codableは非常に強力な機能のひとつです。しかしながらそれを生かすEncoderやDecoderはswift4時点ではあまり標準で用意されていません。ならば自作してみようというテーマです。
Yuichi Nakayasu
May 10, 2018
Transcript
73 slides
-
$PEBCMF 4XJGU͔Β'PVOEBUJPOʹඪ४ࡌ͞Εͨ ಛఆͷϑΥʔϚοτͷσʔλͱಛఆͷΫϥεߏମͱͷ ૬ޓมΛ؆୯ʹߦ͑ΔΑ͏ʹ͢ΔͨΊͷ ඇৗʹڧྗͳϓϩτίϧ /// A type that can
convert itself into and out of an external representation. public typealias Codable = Decodable & Encodable ࣮ࡍ&ODPEBCMFͱ%FDPEBCMFͷ߹ϓϩτίϧ -
$PEBCMF struct SearchCondition { let departure: String let destination: String
let date: Date let stay: Int let persons: Int let keyword: String } let condition = SearchCondition( departure: "Osaka", destination: "Sapporo", date: Date(), stay: 3, persons: 1, keyword: "ே৯͖" ) ྫ ͱ͋ΔཱྀߦݕࡧΞϓϦͷݕࡧ݅ΛೖΕΔͨΊͷߏମ -
$PEBCMF struct SearchCondition: Codable { let departure: String let destination:
String let date: Date let stay: Int let persons: Int let keyword: String } let condition = SearchCondition( departure: "Osaka", destination: "Sapporo", date: Date(), stay: 3, persons: 1, keyword: "ே৯͖" ) ྫ ͱ͋ΔཱྀߦݕࡧΞϓϦͷݕࡧ݅ΛೖΕΔͨΊͷߏମ -
$PEBCMF let encoder = JSONEncoder() let encoded = try! encoder.encode(condition)
let json = String(data: encoded, encoding: .utf8)! 4FBSDI$POEJUJPO+40/จࣈྻʹ͢Δ +40/&ODPEFSʹ4FBSDI$POEJUJPOΛͯ͠Δ͚ͩ { "stay" : 3, "destination" : "Sapporo", "keyword" : "ே৯͖", "departure" : "Osaka", "persons" : 1, "date" : "2018-05-10" } -
$PEBCMF let encoder = JSONEncoder() let encoded = try! encoder.encode(condition)
let json = String(data: encoded, encoding: .utf8)! 4FBSDI$POEJUJPO+40/จࣈྻʹ͢Δ +40/&ODPEFSʹ4FBSDI$POEJUJPOΛͯ͠Δ͚ͩ { "stay" : 3, "destination" : "Sapporo", "keyword" : "ே৯͖", "departure" : "Osaka", "persons" : 1, "date" : "2018-05-10" } -
$PEBCMF let encoder = JSONEncoder() let encoded = try! encoder.encode(condition)
let json = String(data: encoded, encoding: .utf8)! 4FBSDI$POEJUJPO+40/จࣈྻʹ͢Δ +40/&ODPEFSʹ4FBSDI$POEJUJPOΛͯ͠Δ͚ͩ { "stay" : 3, "destination" : "Sapporo", "keyword" : "ே৯͖", "departure" : "Osaka", "persons" : 1, "date" : "2018-05-10" } -
$PEBCMF let encoder = JSONEncoder() let encoded = try! encoder.encode(condition)
let json = String(data: encoded, encoding: .utf8)! 4FBSDI$POEJUJPO+40/จࣈྻʹ͢Δ +40/&ODPEFSʹ4FBSDI$POEJUJPOΛͯ͠Δ͚ͩ { "stay" : 3, "destination" : "Sapporo", "keyword" : "ே৯͖", "departure" : "Osaka", "persons" : 1, "date" : "2018-05-10" } -
$PEBCMF let decoder = JSONDecoder() let decoded = json.data(using: .utf8)!
let condition = try! decoder.decode(SearchCondition.self, from: decoded) +40/จࣈྻ4FBSDI$POEJUJPOʹ͢Δ +40/%FDPEFSʹ+40/จࣈྻΛͯ͠ 4FBSDI$POEJUJPOͷܕΛࢦఆͯ͠Δ͚ͩ -
$PEBCMF let decoder = JSONDecoder() let decoded = json.data(using: .utf8)!
let condition = try! decoder.decode(SearchCondition.self, from: decoded) +40/จࣈྻ4FBSDI$POEJUJPOʹ͢Δ +40/%FDPEFSʹ+40/จࣈྻΛͯ͠ 4FBSDI$POEJUJPOͷܕΛࢦఆͯ͠Δ͚ͩ -
$PEBCMF let decoder = JSONDecoder() let decoded = json.data(using: .utf8)!
let condition = try! decoder.decode(SearchCondition.self, from: decoded) +40/จࣈྻ4FBSDI$POEJUJPOʹ͢Δ +40/%FDPEFSʹ+40/จࣈྻΛͯ͠ 4FBSDI$POEJUJPOͷܕΛࢦఆͯ͠Δ͚ͩ -
$PEBCMF let decoder = JSONDecoder() let decoded = json.data(using: .utf8)!
let condition = try! decoder.decode(SearchCondition.self, from: decoded) +40/จࣈྻ4FBSDI$POEJUJPOʹ͢Δ +40/%FDPEFSʹ+40/จࣈྻΛͯ͠ 4FBSDI$POEJUJPOͷܕΛࢦఆͯ͠Δ͚ͩ -
࡞ͬͯΈ͍ͨͷ 63-2VFSZ*UFN&ODPEFS $PEBCMFͳΦϒδΣΫτΛ͢ͱɺ63-2VFSZ*UFNͷྻΛฦ͢ let condition = SearchCondition( departure: "Osaka", destination:
"Sapporo", date: Date(), stay: 3, persons: 1, keyword: "ே৯͖" ) var components = URLComponents(string: "https:// hogehoge.com")! components.queryItems = try! URLQueryItemEncoder().encode(condition) -
࡞ͬͯΈ͍ͨͷ 63-2VFSZ*UFN&ODPEFS $PEBCMFͳΦϒδΣΫτΛ͢ͱɺ63-2VFSZ*UFNͷྻΛฦ͢ let condition = SearchCondition( departure: "Osaka", destination:
"Sapporo", date: Date(), stay: 3, persons: 1, keyword: "ே৯͖" ) var components = URLComponents(string: "https:// hogehoge.com")! components.queryItems = try! URLQueryItemEncoder().encode(condition) -
࡞ͬͯΈ͍ͨͷ 63-2VFSZ*UFN&ODPEFS $PEBCMFͳΦϒδΣΫτΛ͢ͱɺ63-2VFSZ*UFNͷྻΛฦ͢ let condition = SearchCondition( departure: "Osaka", destination:
"Sapporo", date: Date(), stay: 3, persons: 1, keyword: "ே৯͖" ) var components = URLComponents(string: "https:// hogehoge.com")! components.queryItems = try! URLQueryItemEncoder().encode(condition) -
࡞ͬͯΈ͍ͨͷ 63-2VFSZ*UFN&ODPEFS $PEBCMFͳΦϒδΣΫτΛ͢ͱɺ63-2VFSZ*UFNͷྻΛฦ͢ let condition = SearchCondition( departure: "Osaka", destination:
"Sapporo", date: Date(), stay: 3, persons: 1, keyword: "ே৯͖" ) var components = URLComponents(string: "https:// hogehoge.com")! components.queryItems = try! URLQueryItemEncoder().encode(condition) -
࡞ͬͯΈ͍ͨͷ 63-2VFSZ*UFN%FDPEFS 63-2VFSZ*UFNͷྻΛ͢ͱɺ$PEBCMFͳΦϒδΣΫτΛฦ͢ let urlString = "http://hogehoge.com? departure=Osaka&destination=Sapporo&date=%5B%2254623157 5.650868%22%5D&stay=3&persons=1&keyword=%E6%9C%9D%E9%A3 %9F%E4%BB%98%E3%81%8D"
let components = URLComponents(string: urlString) let condition = try! URLQueryItemDecoder() .decode(SearchCondition.self, from: components?.queryItems) -
࡞ͬͯΈ͍ͨͷ 63-2VFSZ*UFN%FDPEFS 63-2VFSZ*UFNͷྻΛ͢ͱɺ$PEBCMFͳΦϒδΣΫτΛฦ͢ let urlString = "http://hogehoge.com? departure=Osaka&destination=Sapporo&date=%5B%2254623157 5.650868%22%5D&stay=3&persons=1&keyword=%E6%9C%9D%E9%A3 %9F%E4%BB%98%E3%81%8D"
let components = URLComponents(string: urlString) let condition = try! URLQueryItemDecoder() .decode(SearchCondition.self, from: components?.queryItems) -
࡞ͬͯΈ͍ͨͷ 63-2VFSZ*UFN%FDPEFS 63-2VFSZ*UFNͷྻΛ͢ͱɺ$PEBCMFͳΦϒδΣΫτΛฦ͢ let urlString = "http://hogehoge.com? departure=Osaka&destination=Sapporo&date=%5B%2254623157 5.650868%22%5D&stay=3&persons=1&keyword=%E6%9C%9D%E9%A3 %9F%E4%BB%98%E3%81%8D"
let components = URLComponents(string: urlString) let condition = try! URLQueryItemDecoder() .decode(SearchCondition.self, from: components?.queryItems) -
࡞ͬͯΈ͍ͨͷ 63-2VFSZ*UFN%FDPEFS 63-2VFSZ*UFNͷྻΛ͢ͱɺ$PEBCMFͳΦϒδΣΫτΛฦ͢ let urlString = "http://hogehoge.com? departure=Osaka&destination=Sapporo&date=%5B%2254623157 5.650868%22%5D&stay=3&persons=1&keyword=%E6%9C%9D%E9%A3 %9F%E4%BB%98%E3%81%8D"
let components = URLComponents(string: urlString) let condition = try! URLQueryItemDecoder() .decode(SearchCondition.self, from: components?.queryItems) -
ొਓ &ODPEFSϓϩτίϧ protocol Encoder { var codingPath: [CodingKey] { get
} var userInfo: [CodingUserInfoKey : Any] { get } func container<Key>(keyedBy type: Key.Type) -> KeyedEncodingContainer<Key> where Key : CodingKey func unkeyedContainer() -> UnkeyedEncodingContainer func singleValueContainer() -> SingleValueEncodingContainer } -
ొਓ छྨͷ$POUBJOFS protocol Encoder { var codingPath: [CodingKey] { get
} var userInfo: [CodingUserInfoKey : Any] { get } func container<Key>(keyedBy type: Key.Type) -> KeyedEncodingContainer<Key> where Key : CodingKey func unkeyedContainer() -> UnkeyedEncodingContainer func singleValueContainer() -> SingleValueEncodingContainer } ,FZFEίϯςφ 6OLFZFEίϯςφ 4JOHMF7BMVFίϯςφ ࣮ࡍͷม൴Β͕ߦ͏ -
ొਓ छྨͷ$POUBJOFS protocol Encoder { var codingPath: [CodingKey] { get
} var userInfo: [CodingUserInfoKey : Any] { get } func container<Key>(keyedBy type: Key.Type) -> KeyedEncodingContainer<Key> where Key : CodingKey func unkeyedContainer() -> UnkeyedEncodingContainer func singleValueContainer() -> SingleValueEncodingContainer } ,FZFEίϯςφ 6OLFZFEίϯςφ 4JOHMF7BMVFίϯςφ 4USVDU 1SPUPDPM 1SPUPDPM -
ొਓ &ODPEFS ,FZFE&ODPEJOH $POUBJOFS ,FZFE &ODPEJOH $POUBJOFS 1SPUPDPM 6OLFZFE &ODPEJOH
$POUBJOFS 4JOHMF7BMVF &ODPEJOH $POUBJOFS ,FZFE ίϯςφ Ϋϥε 6OLFZFE ίϯςφ Ϋϥε 4JOHMF7BMVF ίϯςφ Ϋϥε -
ొਓ &ODPEFS ,FZFE&ODPEJOH $POUBJOFS ,FZFE &ODPEJOH $POUBJOFS 1SPUPDPM 6OLFZFE &ODPEJOH
$POUBJOFS 4JOHMF7BMVF &ODPEJOH $POUBJOFS ,FZFE ίϯςφ Ϋϥε 6OLFZFE ίϯςφ Ϋϥε 4JOHMF7BMVF ίϯςφ Ϋϥε ߏମ TUSVDUVSF ϓϩτίϧ -
ొਓ %FDPEFS ,FZFE%FDPEJOH $POUBJOFS ,FZFE %FDPEJOH $POUBJOFS 1SPUPDPM 6OLFZFE %FDPEJOH
$POUBJOFS 4JOHMF7BMVF %FDPEJOH $POUBJOFS ,FZFE ίϯςφ Ϋϥε 6OLFZFE ίϯςφ Ϋϥε 4JOHMF7BMVF ίϯςφ Ϋϥε σίʔμಉ༷ -
ొਓ $PEJOH,FZ struct Student: Codable { var studentName: String let
grade: Int let roomNumber: Int enum Key: String, CodingKey { case studentName = "student_name" case grade case roomNumber = "room_number" } } ΤϯίʔυͱσίʔυͰΩʔͱͯ͠͏ܕ $PEBCMFͰετΞυϓϩύςΟͷ໊લΛͬͯ Ωʔ͕ࣗಈతʹܾఆ͢Δ ΧελϚΠζͰ͖Δ -
ొਓ $PEJOH,FZ struct Student: Codable { var studentName: String let
grade: Int let roomNumber: Int enum Key: String, CodingKey { case studentName = "student_name" case grade case roomNumber = "room_number" } } ΤϯίʔυͱσίʔυͰΩʔͱͯ͠͏ܕ $PEBCMFͰετΞυϓϩύςΟͷ໊લΛͬͯ Ωʔ͕ࣗಈతʹܾఆ͢Δ ΧελϚΠζͰ͖Δ -
ίϯςφΛ࣮͢Δ &ODPEFS ,FZFE ίϯςφ 6OLFZFE ίϯςφ 4JOHMF7BMVF ίϯςφ %FDPEFS ,FZFE
ίϯςφ 6OLFZFE ίϯςφ 4JOHMF7BMVF ίϯςφ ͦΕͧΕͭͷίϯςφYͭͷίϯςφ -
ίϯςφΛ࣮͢Δ ڞ௨͢ΔΠϯλʔϑΣΠε var codingPath: [CodingKey] { get } mutating func
nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type, forKey key: Key) -> KeyedEncodingContainer<NestedKey> where NestedKey : CodingKey mutating func nestedUnkeyedContainer(forKey key: Key) -> UnkeyedEncodingContainer mutating func superEncoder() -> Encoder ίʔσΟϯάΩʔܥ ωετίϯςφܥ -
ίϯςφΛ࣮͢Δ ωετίϯςφͱ struct User: Codable { let name: String let
email: String } { “name” : “Nakayasu” “attribute” : { “email” : “[email protected]”, “tel” : “06-1234-5678”, “dept” : “mobile app service” } } ૬ޓม -
ίϯςφΛ࣮͢Δ ωετίϯςφͱ struct User: Codable { let name: String let
email: String } { “name” : “Nakayasu” “attribute” : { “email” : “[email protected]”, “tel” : “06-1234-5678”, “dept” : “mobile app service” } } ૬ޓม -
ίϯςφΛ࣮͢Δ ωετίϯςφͱ struct User: Codable { let name: String let
email: String } { “name” : “Nakayasu” “attribute” : { “email” : “[email protected]”, “tel” : “06-1234-5678”, “dept” : “mobile app service” } } ૬ޓม -
ίϯςφΛ࣮͢Δ struct User: Encodable { let name: String let email:
String enum CodingKeys: String, CodingKey { case name case attribute } enum AttributeCodingKeys: String, CodingKey { case email } func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(name, forKey: .name) var attributeContainer = container.nestedContainer(keyedBy: AttributeCodingKeys.self, forKey: .attribute) try attributeContainer.encode(email, forKey: .email) } } ͠ग़͢ͱ͍͜͠ͷͰׂѪN N -
ίϯςφΛ࣮͢Δ ڞ௨͢ΔΠϯλʔϑΣΠε mutating func encodeNil(forKey key: Key) throws mutating func
encode(_ value: Bool, forKey key: Key) throws mutating func encode(_ value: String, forKey key: Key) throws mutating func encode(_ value: Double, forKey key: Key) throws mutating func encode(_ value: Float, forKey key: Key) throws mutating func encode(_ value: Int, forKey key: Key) throws mutating func encode(_ value: Int8, forKey key: Key) throws mutating func encode(_ value: Int16, forKey key: Key) throws mutating func encode(_ value: Int32, forKey key: Key) throws mutating func encode(_ value: Int64, forKey key: Key) throws mutating func encode(_ value: UInt, forKey key: Key) throws mutating func encode(_ value: UInt8, forKey key: Key) throws mutating func encode(_ value: UInt16, forKey key: Key) throws mutating func encode(_ value: UInt32, forKey key: Key) throws mutating func encode(_ value: UInt64, forKey key: Key) throws mutating func encode<T>(_ value: T, forKey key: Key) throws where T : Encodable Τϯίʔυॲཧܥ -
/JM 4USJOH #PPM *OU छྨ
6*OU छྨ 'MPBU %PVCMF &ODPEBCMF ίϯςφΛ࣮͢Δ mutating func encode(_ value: ܕ, forKey key: Key) throws ڞ௨͢ΔΠϯλʔϑΣΠε ཁϓϦϛςΟϒͳܕΛશ෦ରԠ͠ͳ͍͞Αͱ͍͏͜ͱ ڞ௨ϝιουΛݺͼग़͢Α͏ʹ͓͚ͯ͠Α͍ -
ίϯςφΛ࣮͢Δ ,FZFEίϯςφ lΩʔlʹͳ͍ͬͯΔܗࣜ͜Ε͕ݺΕΔ let condition = SearchCondition( departure : "Osaka",
destination : "Sapporo", date : Date(), stay : 3, persons : 1, keyword : "ே৯͖" ) $PEBCMFͳܕ ϓϩύςΟ໊ Ωʔ ͱͦͷͰͰ͖͍ͯΔͷͰɺ ͓ͷͣͱ,FZFEʹͳ͍ͬͯΔ -
ίϯςφΛ࣮͢Δ ,FZFEίϯςφ private class URLQueryItemEncoderKeyedContainer<Key>: KeyedEncodingContainerProtocol where Key: CodingKey {
private var encoder: URLQueryItemEncoder var codingPath: [CodingKey] { return encoder.codingPath } init(encoder: URLQueryItemEncoder) { self.encoder = encoder } -
ίϯςφΛ࣮͢Δ ,FZFEίϯςφ private class URLQueryItemEncoderKeyedContainer<Key>: KeyedEncodingContainerProtocol where Key: CodingKey {
private var encoder: URLQueryItemEncoder var codingPath: [CodingKey] { return encoder.codingPath } init(encoder: URLQueryItemEncoder) { self.encoder = encoder } ֎ଆ͔ΒଘࡏΒͳ͍͍ͯ͘ͷͰɺQSJWBUFΫϥε -
ίϯςφΛ࣮͢Δ ,FZFEίϯςφ private class URLQueryItemEncoderKeyedContainer<Key>: KeyedEncodingContainerProtocol where Key: CodingKey {
private var encoder: URLQueryItemEncoder var codingPath: [CodingKey] { return encoder.codingPath } init(encoder: URLQueryItemEncoder) { self.encoder = encoder } ΤϯίʔμͷࢀরΛ͓࣋ͬͯ͘ -
ίϯςφΛ࣮͢Δ ,FZFEίϯςφ private func store(_ value: Any, forKey key: Key)
{ let name = key.stringValue let stringValue = String(describing: value) let queryItem = URLQueryItem(name: name, value: stringValue) encoder.append(queryItem: queryItem) } ڞ௨ϝιου -
ίϯςφΛ࣮͢Δ ,FZFEίϯςφ private func store(_ value: Any, forKey key: Key)
{ let name = key.stringValue let stringValue = String(describing: value) let queryItem = URLQueryItem(name: name, value: stringValue) encoder.append(queryItem: queryItem) } ڞ௨ϝιου $PEJOH,FZ͔ΒΩʔ໊ΛTUSJOH7BMVFͰऔಘͰ͖Δ ΩϟϝϧεωʔΫͷม͜͜ͰͰ͖Δ -
ίϯςφΛ࣮͢Δ ,FZFEίϯςφ private func store(_ value: Any, forKey key: Key)
{ let name = key.stringValue let stringValue = String(describing: value) let queryItem = URLQueryItem(name: name, value: stringValue) encoder.append(queryItem: queryItem) } ڞ௨ϝιου 63-ύϥϝʔλจࣈྻͳͷͰɺͲͷܕจࣈྻͰѻ͏ ϓϦϛςΟϒҎ֎৭ʑରࡦ͕ඞཁ -
ίϯςφΛ࣮͢Δ ,FZFEίϯςφ private func store(_ value: Any, forKey key: Key)
{ let name = key.stringValue let stringValue = String(describing: value) let queryItem = URLQueryItem(name: name, value: stringValue) encoder.append(queryItem: queryItem) } ڞ௨ϝιου 63-2VFSZ*UFNΛ࡞ͬͯΤϯίʔμʹྲྀ͠ࠐΈ BQQFOEޙͰΤϯίʔμʹ࣮͢Δ -
ίϯςφΛ࣮͢Δ ,FZFEίϯςφ func encodeNil(forKey key: Key) throws { /* NOP
*/ } func encode(_ value: Bool, forKey key: Key) throws { store(value, forKey: key) } func encode(_ value: String, forKey key: Key) throws { store(value, forKey: key) } func encode(_ value: Double, forKey key: Key) throws { store(value, forKey: key) } func encode(_ value: Float, forKey key: Key) throws { store(value, forKey: key) } func encode(_ value: Int, forKey key: Key) throws { store(value, forKey: key) } func encode(_ value: Int8, forKey key: Key) throws { store(value, forKey: key) } func encode(_ value: Int16, forKey key: Key) throws { store(value, forKey: key) } func encode(_ value: Int32, forKey key: Key) throws { store(value, forKey: key) } func encode(_ value: Int64, forKey key: Key) throws { store(value, forKey: key) } func encode(_ value: UInt, forKey key: Key) throws { store(value, forKey: key) } func encode(_ value: UInt8, forKey key: Key) throws { store(value, forKey: key) } func encode(_ value: UInt16, forKey key: Key) throws { store(value, forKey: key) } func encode(_ value: UInt32, forKey key: Key) throws { store(value, forKey: key) } func encode(_ value: UInt64, forKey key: Key) throws { store(value, forKey: key) } ڞ௨ϝιου -
func encodeNil(forKey key: Key) throws { /* NOP */ }
func encode(_ value: Bool, forKey key: Key) throws { store(value, forKey: key) } func encode(_ value: String, forKey key: Key) throws { store(value, forKey: key) } func encode(_ value: Double, forKey key: Key) throws { store(value, forKey: key) } func encode(_ value: Float, forKey key: Key) throws { store(value, forKey: key) } func encode(_ value: Int, forKey key: Key) throws { store(value, forKey: key) } func encode(_ value: Int8, forKey key: Key) throws { store(value, forKey: key) } func encode(_ value: Int16, forKey key: Key) throws { store(value, forKey: key) } func encode(_ value: Int32, forKey key: Key) throws { store(value, forKey: key) } func encode(_ value: Int64, forKey key: Key) throws { store(value, forKey: key) } func encode(_ value: UInt, forKey key: Key) throws { store(value, forKey: key) } func encode(_ value: UInt8, forKey key: Key) throws { store(value, forKey: key) } func encode(_ value: UInt16, forKey key: Key) throws { store(value, forKey: key) } func encode(_ value: UInt32, forKey key: Key) throws { store(value, forKey: key) } func encode(_ value: UInt64, forKey key: Key) throws { store(value, forKey: key) } ίϯςφΛ࣮͢Δ ,FZFEίϯςφ { store(value, forKey: key) } ڞ௨ϝιου -
ΤϯίʔμΛ࣮͢Δ class URLQueryItemEncoder: Encoder { var codingPath = [CodingKey]() var
userInfo = [CodingUserInfoKey : Any]() private var queryItems: [URLQueryItem]? fileprivate func append(queryItem: URLQueryItem) { if queryItems == nil { queryItems = [] } queryItems!.append(queryItem) } -
ΤϯίʔμΛ࣮͢Δ class URLQueryItemEncoder: Encoder { var codingPath = [CodingKey]() var
userInfo = [CodingUserInfoKey : Any]() private var queryItems: [URLQueryItem]? fileprivate func append(queryItem: URLQueryItem) { if queryItems == nil { queryItems = [] } queryItems!.append(queryItem) } -
ΤϯίʔμΛ࣮͢Δ class URLQueryItemEncoder: Encoder { var codingPath = [CodingKey]() var
userInfo = [CodingUserInfoKey : Any]() private var queryItems: [URLQueryItem]? fileprivate func append(queryItem: URLQueryItem) { if queryItems == nil { queryItems = [] } queryItems!.append(queryItem) } ίϯςφ͔Βݺͼग़ͨ͢ΊͷpMFQSJWBUFͳϝιου -
ΤϯίʔμΛ࣮͢Δ func container<Key>(keyedBy type: Key.Type) -> KeyedEncodingContainer<Key> where Key :
CodingKey { let container = URLQueryItemKeyedEncodingContainer<Key>(encoder: self) return KeyedEncodingContainer(container) } ,FZFEίϯςφΛฦ͢ func unkeyedContainer() -> UnkeyedEncodingContainer { } func singleValueContainer() -> SingleValueEncodingContainer { } ˢ͍͢·ͤΜׂѪ͠·͢N N -
ΤϯίʔμΛ࣮͢Δ func container<Key>(keyedBy type: Key.Type) -> KeyedEncodingContainer<Key> where Key :
CodingKey { let container = URLQueryItemKeyedEncodingContainer<Key>(encoder: self) return KeyedEncodingContainer(container) } ,FZFEίϯςφΛฦ͢ -
ΤϯίʔμΛ࣮͢Δ func container<Key>(keyedBy type: Key.Type) -> KeyedEncodingContainer<Key> where Key :
CodingKey { let container = URLQueryItemKeyedEncodingContainer<Key>(encoder: self) return KeyedEncodingContainer(container) } ,FZFEίϯςφΛฦ͢ -
ΤϯίʔμΛ࣮͢Δ private var queryItems: [URLQueryItem]? func encode<T>(_ value: T) throws
-> [URLQueryItem]? where T : Encodable { try value.encode(to: self) return queryItems } FODPEFϝιουࣗલͰ࡞Δ -
ΤϯίʔμΛ࣮͢Δ private var queryItems: [URLQueryItem]? func encode<T>(_ value: T) throws
-> [URLQueryItem]? where T : Encodable { try value.encode(to: self) return queryItems } FODPEFϝιουࣗલͰ࡞Δ -
ΤϯίʔμΛ࣮͢Δ private var queryItems: [URLQueryItem]? func encode<T>(_ value: T) throws
-> [URLQueryItem]? where T : Encodable { try value.encode(to: self) return queryItems } FODPEFϝιουࣗલͰ࡞Δ public protocol Encodable { public func encode(to encoder: Encoder) throws } -
࡞ͬͯΈ͍ͨͷ 63-2VFSZ*UFN&ODPEFS $PEBCMFͳΦϒδΣΫτΛ͢ͱɺ63-2VFSZ*UFNͷྻΛฦ͢ let condition = SearchCondition( departure: "Osaka", destination:
"Sapporo", date: Date(), stay: 3, persons: 1, keyword: "ே৯͖" ) var components = URLComponents(string: "https:// hogehoge.com")! components.queryItems = try! URLQueryItemEncoder().encode(condition)