Why this string not work. Any and Optionals in dictionary

Hello.
I have a simple expression with Any and is not work. Why?

var dictarray: [[ String : Any ]] = [[ "kek" : nil ]]

\\ Nil is not compatible with expected dictionary value type 'Any'

···

--

And in other hand this is work

var dictarray: [[ String : Any ]] = [[:]] \\ [[:]], dictarray .count = 1

Седых Александр

The problem here is that:

* `nil` is syntactic sugar for `Optional<Wrapped>.none`, where `Optional` is generic on the `Wrapped` type

* You’re not given the compiler any information about `Wrapped`

You could fix this by writing:

var dictarray: [[String: Any]] = [["kek": Optional<Int>.none]]

replacing `Int` with whatever type you’d expect values of `kek` key to be.

Note: This generates a warning because the conversion from a `Optional` to `Any` is a common pitfall.

However, it’s probably better to rethink your overall approach. In most cases arrays of dictionaries of any type are a poor choice in Swift. In my experience most folks encounter problems like this because they’ve read some general data structure (JSON, property list, or whatever) and are working with the low-level import format. IMO it’s better to sort out this problem when you bring the data into Swift. That is:

1. On import, convert `[String:Any]` to some defined-in-Swift model value (class or struct)

2. Write Swift code that works with that model value

3. On export, convert the model value back to the external representation

Share and Enjoy

···

On 23 Jan 2017, at 10:24, Седых Александр via swift-users <swift-users@swift.org> wrote:

I have a simple expression with Any and is not work. Why?

--
Quinn "The Eskimo!" <http://www.apple.com/developer/&gt;
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

There is no universal `nil`. `nil` is typed. That said think of `nil` as a shorthand form for `Optional<YourType>.none`

···

--
Adrian Zubarev
Sent with Airmail

Am 23. Januar 2017 um 11:24:18, Седых Александр via swift-users (swift-users@swift.org(mailto:swift-users@swift.org)) schrieb:

Hello.
I have a simple expression with Any and is not work. Why?

var dictarray: [[String: Any]] = [["kek": nil]]

\\ Nil is not compatible with expected dictionary value type 'Any'

--

And in other hand this is work

var dictarray: [[String: Any]] = [[:]] \\ [[:]], dictarray.count = 1

Седых Александр _______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

I have a simple expression with Any and is not work. Why?

vardictarray: [[String: Any]] = [["kek": nil]]

\\ Nil is not compatible with expected dictionary value type 'Any'

`nil` is just a literal value that doesn't have a specific type. Even though you want to put it in a dictionary whose `Value` type is `Any`, you must first tell the compiler what type your `nil` value is: for example, is it an Optional<Int> or an Optional<String>? Or any other type that conforms to `ExpressibleByNilLiteral`, for that matter — `nil` is not confined to `Optional`.

This works:

var dictarray: [[String: Any]] = [["kek": nil as Int?]]

This compiles but the compiler raises a warning: "Expression implicitly coerced from 'Int?' to Any". To silence the warning you'd need to add another cast:

var dictarray: [[String: Any]] = [["kek": nil as Int? as Any]]