Hey @isuru, thanks for mentioning this. I’ve updated the snippet in the article.
@rae, the reason there is no use of keyDecodingStrategy is because of the _homeworld like properties. Since they’re not following any of the keyDecodingStrategy conventions (neither camel nor snake case) we have to write custom CodingKeys for them, and thus also for our other properties.
As for why we use these underscored properties in the first place: The _homeworld like properties are used to hold the data we get from the API. In this case a URL pointing to an other resource. Next to those, we have a non underscored property that’s a computed property, like this:
public var homeworld: EventLoopFuture<Planet> {
return client.get(URL(string: _homeworld))
}
This means that instead of exposing a URL to our users in the homeworld property, we expose an EventLoopFuture<Planet>, which is more useful and clean, saving the end user from having to then go and fetch the actual Planet object. The _ itself does nothing here, it’s purely a convention picked up from Python where private properties are prefixed with _. I hope this answers your question, if not, feel free to let me know, happy to further explain :]
@tokopedia Thanks for mentioning this! The tutorial was updated a while ago, but I totally forgot to reply back here. Thanks for the ping @shogunkaramazov!
Try to build with docker, after run it with docker, it prompt an error:
[[http_1 | Fatal error: Error raised at top level: Swift.DecodingError.dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: Optional(The operation could not be completed))): file /home/buildnode/jenkins/workspace/oss-swift-5.0-package-linux-ubuntu-18_04/swift/stdlib/public/core/ErrorType.swift, line 200
Thanks for raising @acca & @shogunkaramazov. I did some digging and turns out swapi.co (the service used in the tutorial) has been shut down. A new version of the SWAPI lives at swapi.dev. I will look into updating the project files & tutorial. For now to work around this you can edit the following line SwapiURLBuilder.swift:
// Change
static let baseUrl = "https://swapi.co/api"
// Into
static let baseUrl = "https://swapi.dev/api"