Skip to content
This repository was archived by the owner on Oct 19, 2024. It is now read-only.
This repository was archived by the owner on Oct 19, 2024. It is now read-only.

Feature Request: Support initializing an AnyCodable with an array #70

Open
@samdowd

Description

@samdowd

I'm trying to pass an array into a function that accepts a parameter of type AnyCodable. To do this I need to convert its type to AnyCodable. The only option to do this is AnyCodable.init(arrayLiteral elements: Any...) but passing the array into that function of course treats it as an array with one element of type array ([[1,2,3]] instead of [1,2,3]).

This is a Swift snafu, supporting variadic parameters without supporting spreading has always been awkward, but the commonly accepted solution is to provide two versions of the function with a variadic parameter so consumers of the API can use it no matter what form their data is in. See: https://forums.swift.org/t/explicit-array-splat-for-variadic-functions/11326 for the generally accepted implementation. Basically the variadic function just collects the elements into an array and passes them into the array version of the function, where the actual function implementation is. The current AnyCodable implementation offers only the variadic version of the function.

The possible downside to adding this is that given the necessary vagueness of types in this library, it is possible someone would want the [[1,2,3]] behavior, and adding this would make that more difficult, so it may need to be done differently than the standard format.

If anyone knows of a way to turn an array into an AnyCodable other than the code change I've proposed, I'd be happy to hear it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions