ios - Swift: calling failable initializer from throwing initializer? -


i have need extend struct having failable initializer throwing initializer calling failable initializer. , see no elegant or clear way in swift 3.1.

something this:

extension product: jsondecodable {      public enum error: swift.error {         case unabletoparsejson     }      init(decodefromjson json: json) throws {         guard let jsonobject = json as? jsonobject else {             throw error.unabletoparsejson         }          // meta-code         self.init(dictionary: jsonobject) ?? throw error.unabletoparsejson     } } 

is there elegant , clean way that?

found semi-clean way while writing question:

extension product: jsondecodable {      public enum error: swift.error {         case unabletoparsejson     }      init(decodefromjson json: json) throws {         guard let jsonobject = json as? jsonobject,             let initialized = type(of: self).init(dictionary: jsonobject)         else {             throw error.unabletoparsejson         }          self = initialized     } } 

Comments

Popular posts from this blog

'hasOwnProperty' in javascript -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -

How to understand 2 main() functions after using uftrace to profile the C++ program? -