4
\$\begingroup\$

I'm currently learning Swift and today I created my first application, a simple calculator. My main goal was to practice and create something without any help. After a few hours my application was ready for use but of course not perfect. After overlooking my code I noticed that there are areas which I should improve. For example, I think I could simplify this area:

zahlXS = DisplayX.text!
zahlYS = DisplayY.text!
let zahlX = Int(zahlXS)
let zahlY = Int(zahlYS)

My goal is to take the String variable zahlXS and convert it to an integer variable, so I'm able to calculate with my variables. Please note that I declared zahlXS and zahlYS at the very beginning of my code.

I'm also trying to use the "MVC" concept. In this application, I put my functions add and sub in the ViewController because I first created a struct in a separate file but I just couldn't access the functions in my ViewController. I tried to create a whole class instead of a struct afterwards, but this didn't solve the problem either.

import UIKit
import Foundation

var zahlXS: String = String()
var zahlYS: String = String()

func add(x: Int, y: Int) -> Int {
return x + y
}

func sub(x: Int, y: Int) -> Int {
return x - y
}


class ViewController: UIViewController {

@IBOutlet weak var DisplayX: UITextField!

@IBOutlet weak var DisplayY: UITextField!

@IBOutlet weak var Resultat: UILabel!


@IBAction func performAddition(_ sender: UIButton) {
    zahlXS = DisplayX.text!
    zahlYS = DisplayY.text!
    let zahlX = Int(zahlXS)
    let zahlY = Int(zahlYS)

    let resultAdd = add(x: zahlX!, y: zahlY!)
    Resultat.text = "The result is: \(resultAdd)"



}

@IBAction func performSubtraktion(_ sender: UIButton) {
    zahlXS = DisplayX.text!
    zahlYS = DisplayY.text!
    let zahlX = Int(zahlXS)
    let zahlY = Int(zahlYS)

    let resultSub = sub(x: zahlX!, y: zahlY!)
    Resultat.text = "The result is: \(resultSub)"

}

}
\$\endgroup\$
3
  • \$\begingroup\$ Please consider to improve your quesition title to a short summarization what your code is suppoesd to do. It's pretty meaningless right now. \$\endgroup\$ – πάντα ῥεῖ Apr 13 '17 at 20:03
  • \$\begingroup\$ Hi! Thanks for the tip! Changed it, hope now it's better. Have a good day. \$\endgroup\$ – JVermeer Apr 14 '17 at 7:28
  • \$\begingroup\$ please: always lowercase first letter if it is an instance and not the class definition. also for interface control references \$\endgroup\$ – muescha Apr 20 '17 at 1:12
3
\$\begingroup\$

You had the right idea to use to encapsulate the variables and operations in a class object or struct, rather than using the global scope. It was not clear why this did not work for you.

To make things easy you can declare the object in the same file as the view controller.

E.g.

// Declare a struct to encapsulate the x and y variables.
// Perform the add and subtract operations using the instance variables.
// This code can be moved into Arithmetic.swift
struct Arithmetic {
   let x: Int
   let y: Int

   // Create a convenience initialiser for code which can be reused.
   init(x: String, y: String) {
       self.x = Int(x) ?? 0
       self.y = Int(y) ?? 0
   }

   // Use encapsulation to perform the operations.

   func add() -> Int {
       return x + y
   }

   func subtract() -> Int {
       return x - y
   }
}

class ViewController: UIViewController {

    // You correctly defined the IBOutlets as weak to prevent retain cycles.
    // Property names should begin with lower-case. 
    @IBOutlet weak var displayX: UITextField!
    @IBOutlet weak var displayY: UITextField!
    @IBOutlet weak var resultat: UILabel!


    @IBAction func performAddition(_ sender: UIButton) {

        // Use a guard condition to unwrap the variables.
        // Doing this avoids crashes when you force unwrap a nil.
        guard let x = displayX.text, let y = displayY.text else {
            return
        }

        let a = Arithmetic(x: x, y: y)
        let r = a.add()
        resultat.text = "The result of addition is: \(r)"
    }


    @IBAction func performSubtraktion(_ sender: UIButton) {
        guard let x = displayX.text, let y = displayY.text else {
            return
        }

        let a = Arithmetic(x: x, y: y)
        let r = a.subtract()
        resultat.text = "The result of subtraction is: \(r)"
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ Hi @Luke Van In, thank you for your response! This was very helpful, I really appreciate it. Wish you a nice day! \$\endgroup\$ – JVermeer Apr 14 '17 at 12:10
  • \$\begingroup\$ Int string initializer returns an optional \$\endgroup\$ – Leo Dabus Apr 29 '17 at 3:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.