struct Repo: Mappable {
var id: Int = 0
init(_ map: Map) {
id <- map["id"]
}
}
What you did:
let repo = Mapper<Repo>().map(myJSONDictionary)
What you expected:
print(repo.id) // prints 1 not 0
What you got:
print(repo.id) // prints 0
Hey there! Thanks for the awesome tool to map the data!
Above is a use case when we need to handle string values for basic types. I know, I know that it would be cool to use transforms, but it is not elegant solution to handle a such case.
The proposal:
Simply do next steps
extension Int: StringInitiable {
init?(stringValue: String) {
guard let value = Int(stringValue) else { return nil }
self = value
}
}
Edit operators to handle <T: StringInitiable>
Edit Map's value to handle a case
if let v = currentValue as? String { return T.init(stringValue: currentValue)
Point me if I wrong but it would be awesome to automatically cast String to basic types without any effort for the end user and moreover without breaking changes.
Your JSON dictionary:
Your model:
What you did:
What you expected:
What you got:
Hey there! Thanks for the awesome tool to map the data! Above is a use case when we need to handle string values for basic types. I know, I know that it would be cool to use transforms, but it is not elegant solution to handle a such case. The proposal: Simply do next steps
<T: StringInitiable>
Map
's value to handle a casePoint me if I wrong but it would be awesome to automatically cast
String
to basic types without any effort for the end user and moreover without breaking changes.I am happy to make the PR
Cheers, Vlad