snippetswiftMinor
Convert String to Integer (calculator app)
Viewed 0 times
appconvertstringcalculatorinteger
Problem
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:
My goal is to take the
I'm also trying to use the "MVC" concept. In this application, I put my functions
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)"
}
}Solution
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.
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)"
}
}Code Snippets
// 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)"
}
}Context
StackExchange Code Review Q#160686, answer score: 3
Revisions (0)
No revisions yet.