Method chaining to extract total count of occurrences of a specific value from multiple nested dictionaries

I am trying to extract the number of times a specific string value occurs in a group of nested arrays and would like to use something more concise than an if let loop. Using the filter method I am able to export a dictionary from each of the nested dictionaries with just the string value I am looking for, but I can’t collate the vales to count them all:

var returnCounter: [[String] : Int] = [:]

var mondayFruits: [String : [String : Int]] = [Monday : [Orange : 1], Monday : [Apple : 3]]
var tuesdayFruits: [String : [String : Int]] = [Tuesday : [Banana : 2]]
var wednesdayFruits: [String : [String : Int]] = [Wednesday: [Orange : 1], Wednesday: [Banana : 1]]
var thursdayFruits: [String : [String : Int]] = [Thursday: [Apple : 2], Thursday: [Grapes : 12]]
var fridayFruits: [String : [String : Int]] = [Friday : [Banana : 1], Friday : [Apple : 1]]
								



numberOfBananas = value.filter {$0.key.contains(“Banana”)}
//[[“Banana” ]: 2]
//[[“Banana” ]: 1]
//[[“Banana” ]: 1]

Ideally I wanted to chain methods just to extract the number of occurrences and have everything on a single line but got the error: Ambiguous reference to member 'filter'

numberOfBananas = value.filter {$0.key.contains(“Banana”)}.index(forKey: [“Banana”].count)

What does your value variable look like? There're only mondayFruits - fridayFruits here. They don't even have String quote. It's interesting how you get it to compile.

I am not clear exactly what your question is, but maybe the answer is somewhere in this re‐implementation:

enum Day : CaseIterable {
    case monday
    case tuesday
    case wednesday
    case thursday
    case friday
}
enum Fruit : CaseIterable {
    case apple
    case banana
    case grape
    case orange
}

let records: [Day: [Fruit: Int]] = [
    .monday: [
        .orange: 1,
        .apple: 3
    ],
    .tuesday: [
        .banana: 2
    ],
    .wednesday: [
        .orange: 1,
        .banana: 1
    ],
    .thursday: [
        .apple: 2,
        .grape: 12
    ],
    .friday: [
        .banana: 1,
        .apple: 1
    ]
]

for fruit in Fruit.allCases {
    print(fruit)

    for day in Day.allCases {
        let count = records[day]?[fruit] ?? 0
        // When you want a record for a single day.
        print("\(day): \(count)")
    }

    // When you want the total of all days at once.
    let total = records.values.reduce(into: 0) { $0 += $1[fruit] ?? 0 }
    // Or the same thing more legible but less concise:
    _ = records.values.reduce(into: 0) { (count: inout Int, dayRecord: [Fruit: Int]) in
        count += dayRecord[fruit] ?? 0
    }

    print("Total: \(total)")
    print()
}

(I used enumerations (1) to get easy access to a master list of allCases in order to iterate them during printing, and (2) so I could not accidentally enter a Day as a Fruit or vice‐versa. If those don’t matter and you need them to be String instances for some other reason, the same query logic will still work.)

I'd like to formally request this thread be deleted, I will post compilable code again.

Apologies for this thread an to those who took the time to help, but I thought it best to start again.

A whiteboard moment.

That’s fine.

And I did see your request to have it deleted, but I am not capable of it. (Well, I suppose I could accomplish that by flagging it as a violation of the code of conduct, but that would have other ramifications you don’t want...)

Deleting things isn’t normal on these forums, but I have occasionally seen things marked something like “This post has been withdrawn by its author. It will be removed in x hours unless flagged.” I assume that means a post’s own author has the ability to delete it. I also see a trash can icon under the “...” for posts I’ve authored, so maybe that is what you would have to click if you really want to get rid of this.

I did try deleting it but I don't have permission. I think that flagging it will cause me more issues than I care to deal with, so I'll just leave it and be more cautious next time.

Thanks.