Last active
March 17, 2016 13:54
-
-
Save 15458434/08989e7f2d303131d13b to your computer and use it in GitHub Desktop.
Sum of prices in an NSArray of items.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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