Sum the Values of Nested Custom Objects in Swift
<p>I am trying to add up the values found within some custom objects in Swift. I have the follow code working in a Playground:</p>

<pre><code>//Custom Object
class EntryPack: NSObject{
  var date: String!
  var duration: Double!
  var customValues:[CustomValuePack] = [] //Nested array of objects declared below
}

//Another object to nest in the above
class CustomValuePack: NSObject{
  var name:String!
  var value: Double!
}

//Instantiate nested objects
let cv1 = CustomValuePack()
  cv1.name = "Day"
  cv1.value = 1.2
let cv2 = CustomValuePack()
  cv2.name = "Day"
  cv2.value = 1.3
let cv3 = CustomValuePack()
  cv2.name = "Night"
  cv2.value = 2.2

//Instantiate parent objects
let entry1 = EntryPack()
  entry1.date = "bingo"
  entry1.duration = 1.1

  entry1.customValues = [cv1, cv2]

let entry2 = EntryPack()
  entry2.date = "bingo"
  entry2.duration = 2.2

let entry3 = EntryPack()
  entry3.date = "dang"
  entry3.duration = 3.0

//Throw it all together into an array
var package = [entry1, entry2, entry3]

//Predicate for some filtering
let predicate = NSPredicate(format:"date = %@", "bingo")
let results = (package as NSArray).filteredArrayUsingPredicate(predicate) as! [EntryPack]

//Sum all the parent object durations
let sum = results.reduce(0.0) { $0 + $1.duration }

print(sum) // = 3.3
</code></pre>

<p>But now I want to add up the <code>value</code> for each <code>CustomValuePack</code> where the <code>name</code> is <code>"Day"</code>.</p>

<p>I can't figure out how to do a similar reduce on a nested array of objects. I've tried something like this, but it yields a syntax error:</p>

<pre><code>let sum2 = results.reduce(0.0) { $0.customValues + $1.customValues.value } //Error
</code></pre>

<p><strong>How do I sum the value in a nested array of objects?</strong> I eventually want to apply an NSPredicate to filter by <code>name = Day</code> as well, but I'm not that far yet.</p>