@@ -27,7 +27,7 @@ En | [中文](https://iwill.im/ExCodable/)
2727
2828- Extends Swift ` Codable ` - ` Encodable & Decodable ` ;
2929- Supports Key-Mapping via ` KeyPath ` and Coding-Key:
30- - ExCodable did not read/write memory via unsafe pointers;
30+ - ` ExCodable ` did not read/write memory via unsafe pointers;
3131 - No need to encode/decode properties one by one;
3232 - Just requires using ` var ` to declare properties and provide default values;
3333 - In most cases, the ` CodingKey ` type is no longer necessary, because it will only be used once, ` String ` literals may be better.
@@ -155,27 +155,50 @@ static var keyMapping: [KeyMap<Self>] = [
155155]
156156```
157157
158- ### 6. Custom encode/decode handlers :
158+ ### 6. Custom encode/decode:
159159
160160``` swift
161- static var keyMapping: [KeyMap<Self >] = [
162- KeyMap (\.int , to : " int" ),
163- KeyMap (\.string , to : " string" , encode : { (encoder, stringKeys, test, keyPath) in
164- encoder[stringKeys.first ! ] = " dddd"
165- }, decode : { (test, keyPath, decoder, stringKeys) in
166- switch test.int {
167- case 100 : test.string = " Continue"
168- case 200 : test.string = " OK"
169- case 304 : test.string = " Not Modified"
170- case 403 : test.string = " Forbidden"
171- case 404 : test.string = " Not Found"
172- case 418 : test.string = " I'm a teapot"
173- case 500 : test.string = " Internal Server Error"
174- case 200 ..< 400 : test.string = " success"
175- default : test.string = " failure"
161+ struct TestHandlers : Equatable {
162+ var int: Int = 0
163+ var string: String ?
164+ }
165+ ```
166+
167+ ``` swift
168+ extension TestHandlers : ExCodable {
169+
170+ private enum Keys : String , CodingKey {
171+ case int , string
172+ }
173+ private static let dddd = " dddd"
174+
175+ static var keyMapping: [KeyMap<Self >] = [
176+ KeyMap (\.int , to : Keys.int ),
177+ KeyMap (\.string , to : Keys.string )
178+ ]
179+
180+ init (from decoder : Decoder) throws {
181+ decode (with : Self .keyMapping , using : decoder)
182+ if string == nil || string == Self .dddd {
183+ switch int {
184+ case 100 : string = " Continue"
185+ case 200 : string = " OK"
186+ case 304 : string = " Not Modified"
187+ case 403 : string = " Forbidden"
188+ case 404 : string = " Not Found"
189+ case 418 : string = " I'm a teapot"
190+ case 500 : string = " Internal Server Error"
191+ case 200 ..< 400 : string = " success"
192+ default : string = " failure"
193+ }
176194 }
177- })
178- ]
195+ }
196+ func encode (to encoder : Encoder) throws {
197+ encode (with : Self .keyMapping , using : encoder)
198+ encoder[Keys.string ] = Self .dddd
199+ }
200+
201+ }
179202```
180203
181204### 7. Encode/decode constant properties with subscripts:
@@ -255,37 +278,36 @@ XCTAssertEqual(copy2, test)
255278- [ Swift Package Manager] ( https://swift.org/package-manager/ ) :
256279
257280``` swift
258- .package (url : " https://github.com/iwill/ExCodable" , from : " 0.2 " )
281+ .package (url : " https://github.com/iwill/ExCodable" , from : " 0.4.0 " )
259282```
260283
261284- [ CocoaPods] ( http://cocoapods.org ) :
262285
263286``` ruby
264- pod ' ExCodable' , ' ~> 0.2 '
287+ pod ' ExCodable' , ' ~> 0.4.0 '
265288```
266289
267290- Code Snippets:
268291
292+ > Title: ExCodable
293+ > Summary: Adopte to ExCodable protocol
269294> Language: Swift
270295> Platform: All
271- > Completion: excodable
296+ > Completion: ExCodable
272297> Availability: Top Level
273298
274299``` swift
275300< #extension / struct/ class#> < #Type #> : ExCodable {
276-
277- static var < #keyMapping #> : [KeyMap<< #Type #>> ] = [
301+ static var < #keyMapping #> : [KeyMap<< #SelfType #>> ] = [
278302 KeyMap (\.< #property #> , to : < #" key"# > ),
279303 < #... #>
280304 ]
281-
282305 init (from decoder : Decoder) throws {
283- decode (with : Self .< #keyMapping #> , using : decoder)
306+ decode< #Reference # > (with : Self .< #keyMapping #> , using : decoder)
284307 }
285308 func encode (to encoder : Encoder) throws {
286309 encode (with : Self .< #keyMapping #> , using : encoder)
287310 }
288-
289311}
290312```
291313
0 commit comments