简体   繁体   中英

Swift: How to Create Array Holding Dictionaries

I'm teaching myself swift and am trying to create an array holding dictionaries. The dictionaries are in the following format: String: Any (usually Strings, but sometimes additional arrays of dictionaries).

Is this the correct way to create an array capable of holding dictionaries?

var contentArray: [Dictionary<String, Any>] = []

I went through the question history and saw your edit. indexOfObjectPassingTest is a method on NSArray . You are asking about Array which is a different (though related) type.

Here's how to declare the array:

var contentArray = [[String: Any]]()

Here's how to get the index of an item:

let index1 = contentArray.index(of: aDict)
let index2 = contentArray.index(where: { (some conditions) })

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