Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Float mapping with downcasting (#142)
The behavior of `NSNumber as? Float` in Swift is such that some values that cannot "safely express the value stored" in NSNumber. This means only using `as?` to cast to `Float` can fail, where doing `.floatValue` or `Float.init` would succeed. To work around this oddity, this new `Convertible` conformance calls `.floatValue` on `NSNumber`, leading to a float, that is possibly less precise than you'd like.
- Loading branch information
Showing
5 changed files
with
41 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import Foundation | ||
|
||
// Float convertible implementation | ||
extension Float: Convertible { | ||
public static func fromMap(_ value: Any) throws -> Float { | ||
if let value = value as? Float { | ||
return value | ||
} | ||
|
||
if let object = value as? NSNumber { | ||
return object.floatValue | ||
} | ||
|
||
throw MapperError.convertibleError(value: value, type: Float.self) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters