Dan
Dan

Reputation: 3103

AnyValue in Swift Dictionary

I'd like to represent a dictionary like this:

[
   angles: [first: 1.2, second: 2.2],
   other: 3
]

Is this possible in iOS Swift?

Upvotes: 1

Views: 393

Answers (1)

Nate Cook
Nate Cook

Reputation: 93276

You have to use strings as keys, but otherwise yes -- you can declare it explicitly as a [String: AnyObject] dictionary:

let dict: [String: AnyObject] = [
    "angles": ["first": 1.2, "second": 2.2],
    "other": 3
]

or let type inference decide it's an NSDictionary:

let dict = [
    "angles": ["first": 1.2, "second": 2.2],
    "other": 3
]

Note that you'll have plenty of hassles dealing with this data structure later - if possible define a struct that describes your data instead.

Upvotes: 2

Related Questions