Creating a Swift application computing for the area of a rectangle [closed]

There are a few things wrong here. You’re trying to assign and Int to something that expects a String. You’re also trying to call an instance method of your Computation class without any instance of this class as the receiver. On top of that, you would have to have actually assigned values to the properties on said instance for this to have even worked. I believe this is what you meant to do.

class SecondViewController: UIViewController {
    @IBOutlet weak var buttonPressed: UIButton!
    @IBOutlet weak var inputLength: UITextField!
    @IBOutlet weak var inputWidth: UITextField!
    @IBOutlet weak var display: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
    }        

    func displayComputation(sender: UIButton) {
        let computationObject = Computation()

        computationObject.inputWidth = inputWidth.text.toInt()!
        computationObject.inputLength = inputLength.text.toInt()!

        display.text = String(format: "%d", computationObject.rectanglePerimeter())
    }
}

Note: Your instances should have lowerCamelCase, and your classes should have UpperCamelCase.

It also appears that you’ve made a mistake in the formula if you want to compute area. What you were doing computes perimeter. This is what you want.

class Computation {
    var buttonPressed = 0
    var inputLength = 0
    var inputWidth = 0

    func rectanglePerimeter() -> Int {
        return inputWidth * 2 + inputLength * 2
    }
}

Leave a Comment