Codable ‘has no initializers’ in Xcode 9.3 (Swift 4.1)

As mentioned in the comments, I had to do two things: changing Compilation Mode to Whole Module inside Project settings/Build Settings: reordering the files under Project settings/Build Phases/Compile Sources. Specifically, I brought the files that had an error to the front of the list. Protip: if you search for the name of the file and … Read more

Swift String escaping when serializing to JSON using Codable

You can use .withoutEscapingSlashes option to json decoder to avoid escaping slashes let user = User(username: “John”, profileURL: “http://google.com”) let jsonEncoder = JSONEncoder() jsonEncoder.outputFormatting = .withoutEscapingSlashes let json = try? jsonEncoder.encode(user) if let data = json, let str = String(data: data, encoding: .utf8) { print(str) } Console O/P {“profileURL”:”http://google.com”,”username”:”John”} NOTE: As mention by Martin R … Read more

Swift 4 Decodable – Dictionary with enum as key

The problem is that Dictionary‘s Codable conformance can currently only properly handle String and Int keys. For a dictionary with any other Key type (where that Key is Encodable/Decodable), it is encoded and decoded with an unkeyed container (JSON array) with alternating key values. Therefore when attempting to decode the JSON: {“dictionary”: {“enumValue”: “someString”}} into … Read more

What is difference between optional and decodeIfPresent when using Decodable for JSON Parsing?

There’s a subtle, but important difference between these two lines of code: // Exhibit 1 foo = try container.decode(Int?.self, forKey: .foo) // Exhibit 2 foo = try container.decodeIfPresent(Int.self, forKey: .foo) Exhibit 1 will parse: { “foo”: null, “bar”: “something” } but not: { “bar”: “something” } while exhibit 2 will happily parse both. So in … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)