Skip to content

Instantly share code, notes, and snippets.

@angelabauer
Created August 27, 2019 11:22
Show Gist options
  • Save angelabauer/8142e8361bfbf3b66ad3daf91a3a0221 to your computer and use it in GitHub Desktop.
Save angelabauer/8142e8361bfbf3b66ad3daf91a3a0221 to your computer and use it in GitHub Desktop.
import UIKit
struct CalculatorBrain {
var bmi: BMI?
func getBMIValue() -> String {
let bmiTo1DecimalPlace = String(format: "%.1f", bmi?.value ?? "0.0")
return bmiTo1DecimalPlace
}
//1. Create the getAdvice() method to fetch bmi.advice, returns "No Advice" if bmi is nil.
func getAdvice() -> String {
return bmi?.advice ?? "No Advice"
}
//2. Create the getColor() method to fetch bmi.color, returns a white color if bmi is nil.
func getColor() -> UIColor {
return bmi?.color ?? #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
}
mutating func calculateBMI(height: Float, weight: Float) {
let bmiValue = weight / (height * height)
if bmiValue < 18.5 {
bmi = BMI(value: bmiValue, advice: "Eat more pies!", color: #colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1))
} else if bmiValue < 24.9 {
bmi = BMI(value: bmiValue, advice: "Fit as fiddle!", color: #colorLiteral(red: 0.721568644, green: 0.8862745166, blue: 0.5921568871, alpha: 1))
} else {
bmi = BMI(value: bmiValue, advice: "Go easy on the snacks.", color: #colorLiteral(red: 0.9098039269, green: 0.4784313738, blue: 0.6431372762, alpha: 1))
}
}
}
import UIKit
class ResultViewController: UIViewController {
var bmiValue: String?
//3. Create the advice and color properties as Optionals
var advice: String?
var color: UIColor?
@IBOutlet weak var bmiLabel: UILabel!
@IBOutlet weak var adviceLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
bmiLabel.text = bmiValue
//4. set the advice label text as the advice String.
adviceLabel.text = advice
//5. Set the background color of the view as the color property value.
view.backgroundColor = color
}
@IBAction func recalculatePressed(_ sender: UIButton) {
dismiss(animated: true, completion: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment