简体   繁体   中英

Can't get dictionary item by index - cannot subscript String with an Int

I have a dictionary where key is string and value is array of strings.

var someItems : [String: [String]] = [String: [String]]()

I am trying to get item by it's index...

var temp = someItems[0]

But I am gettings error:

'subscript' is unavailable: cannot subscript String with an Int

I don't understand why this doesn't work?

Dictionary是基于key的而不是基于index的,并且键被声明为String

var temp = someItems["key"]

Dictionary items are not sorted and therefore you can't call them with index. You should be able to access its value by writing someItems ["theString"], where theString is the selected key.

try this

if let array = someItems["your key for array"] as NSArray{

  print(array)

}

it will return your array for the entered key

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM