Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Tip Calculator - NSDecimalNumber and Binary Operators

I tried to use binary operators on NSDecimalNumbers, to make it work I used functions with the binary operators as the name of the functions ("/", "+", and "*"). For whatever reason I get fixit errors on each function making the code unable to compile.

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var billAmountLabel: UILabel!
@IBOutlet weak var customTipPercentLabel1: UILabel!
@IBOutlet weak var customTipPercentageSlider: UISlider!
@IBOutlet weak var customTipPercentLabel2: UILabel!
@IBOutlet weak var tip15Label: UILabel!
@IBOutlet weak var tipCustomLabel: UILabel!
@IBOutlet weak var total15Label: UILabel!
@IBOutlet weak var totalCustomLabel: UILabel!
@IBOutlet weak var inputTextField: UITextField!

let decimal100 = NSDecimalNumber(string: "100.0")
let decimal15Percent = NSDecimalNumber(string: "0.15")

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    inputTextField.becomeFirstResponder()
}

@IBAction func calculateTip(sender: AnyObject) {
    let inputString = inputTextField.text
    let sliderValue = NSDecimalNumber(integer: Int(customTipPercentageSlider.value))
    let customPercent = sliderValue / decimal100

    if sender is UISlider {
        customTipPercentLabel1.text = NSNumberFormatter.localizedStringFromNumber(customPercent, numberStyle: NSNumberFormatterStyle.PercentStyle)
    }

    if !inputString.isEmpty {
        let billAmount = NSDecimalNumber(string: inputString) / decimal100
    }

    if sender is UITextField {
        billAmountLabel.text = " " + formatAsCurrency(billAmount)

        let fifteenTip = billAmount * decimal15Percent
        tip15Label.text = formatAsCurrency(fifteenTip)
        totatal15Label.text = formatAsCurrency(billAmount + fifteenTip)


        let customTip = billAmount * customPercent
        tipCustomLabel.text = formatAsCurrency(customTip)
        totalCustomLabel.text = formatAsCurrency(billAmount + customTip)

        else
            billAmountLabel.text = " "
            tip15Label.text = " "
            total15Label.text = " "
            tipCustomLabel.text = " "
            totalCustomLabel.text = " "


        }
    }

func formatAsCurrency(number: NSNumber) -> String {
        return NSNumberFormatter.localizedStringFromNumber(number, numberStyle: NSNumberFormatterStyle.CurrencyStyle)
}
func  +(left: NSDecimalNumber, right: NSDecimalNumber) -> NSDecimalNumber {
        return left.decimalNumberByAdding(right)
}

func *(left: NSDecimalNumber, right: NSDecimalNumber) -> NSDecimalNumber {
        return left.decimalNumberByMultiplyingBy(right)
}

func /(left: NSDecimalNumber, right NSDecimalNumber) -> NSDecimalNumber {
        return left.decimalNumberByDividingBy(right)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}