Files
swift-openfoodfacts-sdk/Sources/OpenFoodFactsSDK/Extensions/KeyDecodingContainer+Helpers.swift
2025-12-06 18:13:36 +01:00

23 lines
545 B
Swift

extension KeyedDecodingContainer {
public func decodeFloatOrString(forKey key: Key) throws -> Float? {
if let floatVal = try? decode(Float.self, forKey: key) {
return floatVal
}
if let stringVal = try? decode(String.self, forKey: key) {
return Float(stringVal)
}
return nil
}
public func decodeStringOrInt(forKey key: Key) throws -> Int? {
if let intVal = try? decode(Int.self, forKey: key) {
return intVal
}
if let stringVal = try? decode(String.self, forKey: key) {
return Int(stringVal)
}
return nil
}
}