<div dir="ltr"><div>Codable is great and I'm moving to it, but sometimes server return numbers as a string and I have to force the parser to take care of this behavior.</div><div><br></div><div>I would like your opinion if this is the better solution to parse numbers even when I got string.</div><div><br></div><div>Thanks,</div><div><br></div><div>Solli Honorio</div><div><br></div><div><code></div><div><font face="monospace, monospace">import Foundation</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">extension KeyedDecodingContainer {<span style="white-space:pre-wrap">        </span></font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap">        </span>public func decode(_ type: Int.Type, forKey key: Key) throws -> Int {</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap">                </span>guard let stringValue = try? self.decodeIfPresent(String.<wbr>self, forKey: key) else {</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap">                        </span>return try self.decode(Int.self, forKey: key)</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap">                </span>}</font></div><div><span style="white-space:pre-wrap"><font face="monospace, monospace">                </font></span></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap">                </span>guard let integerValue = Int.init(stringValue!) else {</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap">                        </span>return try self.decode(Int.self, forKey: key)</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap">                </span>}</font></div><div><span style="white-space:pre-wrap"><font face="monospace, monospace">                </font></span></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap">                </span>return integerValue</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap">        </span>}</font></div><div><span style="white-space:pre-wrap"><font face="monospace, monospace">        </font></span></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap">        </span>public func decodeIfPresent(_ type: Int.Type, forKey key: K) throws -> Int? {</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap">                </span>guard let stringValue = try? self.decodeIfPresent(String.<wbr>self, forKey: key) else {</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap">                        </span>return try self.decodeIfPresent(Int.self, forKey: key)</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap">                </span>}</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap">                </span>guard let integerValue = Int.init(stringValue!) else {</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap">                                        </span>return try self.decodeIfPresent(Int.self, forKey: key)</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap">                </span>}</font></div><div><span style="white-space:pre-wrap"><font face="monospace, monospace">                </font></span></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap">                </span>return integerValue</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap">        </span>}</font></div><div><font face="monospace, monospace">}</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">let jsonString = """</font></div><div><font face="monospace, monospace">{</font></div><div><font face="monospace, monospace"> "id": "1",</font></div><div><font face="monospace, monospace"> "string": "string",</font></div><div><font face="monospace, monospace"> "integer": "42",</font></div><div><font face="monospace, monospace"> "type": "locations",</font></div><div><font face="monospace, monospace"> "boolean": true</font></div><div><font face="monospace, monospace">}</font></div><div><font face="monospace, monospace">"""</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">let jsonData = jsonString.data(using: .utf8)!</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">struct JSONEntity: Codable {</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap">        </span>let id: Int?</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap">        </span>let string: String</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap">        </span>let integer: Int</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap">        </span>let type: String</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap">        </span>let boolean: Bool</font></div><div><font face="monospace, monospace">}</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">let decoder = JSONDecoder()</font></div><div><font face="monospace, monospace">let foo = try! decoder.decode(JSONEntity.<wbr>self, from: jsonData)</font></div><div><font face="monospace, monospace">debugPrint(foo)</code><br></font></div><div><br></div>-- <br><div class="gmail-m_3024123854825531652gmail_signature">"o animal satisfeito dorme". - Guimarães Rosa</div>
</div>