EDIT
My original answer missed the point of your question it seems, here is what you want:
let minutes = posts.reduce(([String : Int]())) { accumulator, value in
var currentData = accumulator
if accumulator.keys.contains(value.uid) {
currentData[value.uid] = accumulator[value.uid]! + value.minutes
return currentData
} else {
currentData[value.uid] = value.minutes
return currentData
}
}
This is playground code for totalling only the minutes of the unique uid:
let posts = [Post(key: "key", uid: "0", minutes: 5, name: "name"), Post(key: "key", uid: "1", minutes: 5, name: "name"), Post(key: "key", uid: "1", minutes: 5, name: "name"), Post(key: "key", uid: "1", minutes: 5, name: "name"), Post(key: "key", uid: "3", minutes: 2, name: "name")]
let count = posts.reduce((0, [String]())) { accumulator, value in
if accumulator.1.contains(value.uid) {
return accumulator
} else {
return (accumulator.0 + value.minutes, accumulator.1 + [value.uid])
}
}.0
The reduce lets you "iterate" over your posts array. You start with a minute count of 0 and and an empty uid array. If the uid is there then you don't add any minute to the minute count and you leave the array as is. If not, you add the minutes and add the string to the array of uid strings. At the end you get the minute count by getting the first element of the tuple (hence the .0)
the shorter version is:
let count = posts.reduce((0, [String]())) {
return $0.1.contains($1.uid) ? $0 : ($0.0 + $1.minutes, $0.1 + [$1.uid])
}.0
I am having trouble totaling the minutes based on uidYou want to sum of minutes with specific uids? If yes add details of that also in your question.