decodeBoolAsIntOrString (SwifterSwiftSwiftStdlib/KeyedDecodingContainerExtensions.swift)

SwifterSwift · Sources/SwifterSwiftSwiftStdlib/KeyedDecodingContainerExtensions.swift · swift · MIT
dev-tools

Adds decoding helpers to decode booleans from ints or strings in Swift's KeyedDecodingContainer.

// Source: https://github.com/SwifterSwift/SwifterSwift | License: MIT | Copyright 2015-2018 SwifterSwift (https://github.com/swifterswift)
// Provided via justclaw.it.com; keep this notice with the code.
// KeyedDecodingContainerExtensions.swift - Copyright 2026 SwifterSwift

#if canImport(Foundation)
import Foundation
#endif

public extension KeyedDecodingContainer {
    #if canImport(Foundation)
    /// SwifterSwift: Try to decode a Bool as Int then String before decoding as Bool.
    ///
    /// - Parameter key: Key.
    /// - Returns: Decoded Bool value.
    /// - Throws: Decoding error.
    func decodeBoolAsIntOrString(forKey key: Key) throws -> Bool {
        if let intValue = try? decode(Int.self, forKey: key) {
            return (intValue as NSNumber).boolValue
        } else if 

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key