简体   繁体   中英

Return number of arrays within a dictionary key?

How do I return the number of arrays within a dictionary key?

var timedic = [String: [AnyObject]]()

output: var output = ["March 27": ["do the dishes", "take out the trash"], ["Take bro to soccer practice"], "March 29": ["Walk the dog", "Water the plants"], "March 28": ["Clean the house"]]

I tried timedic["March 27"].count but I get the number of objects inside the array instead of the number of arrays under the key "March 27"

If you wanted to have your key map to an Array of Arrays, you would need to do this:

var timedic = [String : [[AnyObject]]]()

Which would map a String to an Array of Arrays of AnyObject. As it currently stands, your dictionary maps a String to an Array of AnyObject.

I'm not sure I understand your question however given this dictionary

var timedic = [
    "March 27": ["do the dishes", "take out the trash"],
    "March 29": ["Walk the dog", "Water the plants"],
    "March 28": ["Clean the house"]
]

If you want the total number of arrays inside the dictionary

let count = timedic.values.count

If you want the total number of String(s) into the dictionary

let count = timedic.values.flatten().count

If you want the total number of String(s) for a given key

let count = timedic["March 27"].values.flatten().count

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