ikevin8me
ikevin8me

Reputation: 4313

How to declare a dictionary using String as key, and Arrays of Ints as value in Swift language?

How do I declare a dictionary using String as key, and containing Arrays as value in Swift?

This won't work:

var unloadedImagesRows:[String:[Int]()]! = [String:[Int]()]()

Upvotes: 0

Views: 67

Answers (3)

ryantxr
ryantxr

Reputation: 4219

var unloadedImagesRows:[String: [Int]] = ["somekey": [1,2,3]]

or for an empty dictionary

var unloadedImagesRows:[String: [Int]] = [ : ]

Upvotes: 1

Twitter khuong291
Twitter khuong291

Reputation: 11682

This will work:

var unloadedImagesRows:[String:[Int]]! = [String:[Int]]()

Upvotes: 1

Code Different
Code Different

Reputation: 93161

You don't need to explicitly declare the type of the variable. Swift is often smart enough to infer the type from its value.

Try this:

var unloadedImagesRows = [String: [Int]]()
unloadedImagesRows["array1"] = [1,2,3,4]
unloadedImagesRows["array2"] = [5,6,7]

Upvotes: 2

Related Questions