[swift-users] Getting ambiguous error only when override Int decoder

Solli Honorio shonorio at gmail.com
Wed Nov 8 00:20:58 CST 2017


I'm trying to override some decode parsers from KeyedDecodingContainer, but
I getting a stranger ambiguous error. Is stranger for me, because I'm
getting this message error only on *Int* and *Double* override parse.

I appreciate any help because I don't know why the Float parse works and
Int got the error.

This is my code

<code>
import Foundation

public extension KeyedDecodingContainer  {

    public func decode(_ type: Float.Type, forKey key: Key) throws -> Float
{
        guard let stringValue = try? self.decodeIfPresent(String.self,
forKey: key) else {
            return try self.decode(Float.self, forKey: key)
        }

        guard let floatValue = Float(stringValue!) else {
            return try self.decode(Float.self, forKey: key)
        }

        return floatValue
    }

    public func decodeIfPresent(_ type: Float.Type, forKey key: Key) throws
-> Float? {
        guard let stringValue = try? self.decodeIfPresent(String.self,
forKey: key) else {
            return try self.decodeIfPresent(Float.self, forKey: key)
        }

        guard let floatValue = Float(stringValue!) else {
            return try self.decodeIfPresent(Float.self, forKey: key)
        }

        return floatValue
    }

    public func decodeIfPresent(_ type: Int.Type, forKey key: Key) throws
-> Int? {
        guard let stringValue = try? self.decodeIfPresent(String.self,
forKey: key) else {
            return try self.decodeIfPresent(Int.self, forKey: key)
        }

        guard let integerValue = Int(stringValue!) else {
            return try self.decodeIfPresent(Int.self, forKey: key)
        }

        return integerValue
    }
}
</code>

... and I got this error on compilation.

<error>

<unknown>:0: error: ambiguous use of 'decodeIfPresent(_:forKey:)'

Swift.KeyedDecodingContainer:180:17: note: found this candidate

    public func decodeIfPresent(_ type: Int.Type, forKey key:
KeyedDecodingContainer.Key) throws -> Int?

                ^

Pimenta.KeyedDecodingContainer<K>:4:17: note: found this candidate

    public func decodeIfPresent(_ type: Int.Type, forKey key:
KeyedDecodingContainer.Key) throws -> Int?

</error >


-- 
"o animal satisfeito dorme". - GuimarĂ£es Rosa
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20171108/db4bbd17/attachment.html>


More information about the swift-users mailing list