Skip to content

Instantly share code, notes, and snippets.

@15458434
Last active March 17, 2016 13:54
Show Gist options
  • Save 15458434/08989e7f2d303131d13b to your computer and use it in GitHub Desktop.
Save 15458434/08989e7f2d303131d13b to your computer and use it in GitHub Desktop.
Sum of prices in an NSArray of items.
// Playground - noun: a place where people can play
import Foundation
class Item: NSObject {
var name: String?
var price: NSDecimalNumber?
init(name: String, price: NSDecimalNumber) {
self.name = name
self.price = price
super.init()
}
}
let one = Item(name: "iPhone 5", price: NSDecimalNumber(string: "100"))
let two = Item(name: "iPhone 6", price: NSDecimalNumber(string: "200"))
let three = Item(name: "iPhone 6s", price: NSDecimalNumber(string: "300"))
let oldTypeOfArray: NSArray = [one, two, three]
let sum = oldTypeOfArray.valueForKeyPath("@sum.price")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment