将代码从视图控制器中移出并将其移动到执行Swift所有function的“Brain”

最近,我经历了很多错误,完成了我的第一个应用程序(它是一个计算器),我发现在ViewController里面执行任何计算通常不是明智的做法,而是在一个单独的文件中完成。 我的主要问题是:

是可以把我的每个函数中的当前代码放入一个类,在迅速将执行它们呢?

然后我可以在视图控制器中的每个函数下实现class.function()。

我之所以要这样做,是因为将来我可以有一个计算器课程,而且我也知道如何在未来的每一个项目中做到这一点,我也可以使用这些课程。

我试过试验,但我似乎无法引用我的其他文件lbl.output。

// // ViewController.swift // MyCalculator // // Created by Kevin Maldjian on 2/6/17. // Copyright © 2017 Kevin Maldjian. All rights reserved. // import UIKit import Foundation class ViewController: UIViewController { var num1 = "" var num2 = "" var finalString = "" var isFirstNumber = true var safeToAdd = true var hasFinalString = false var isClear = true var hasDecimal = false var doubleChecker = 0 var hasSecondDecimal = false @IBOutlet weak var LBLOutput: UILabel! @IBAction func btrNumberClick(_ sender: UIButton) { if isClear { LBLOutput.text = "" isClear = false } let currentText = LBLOutput.text! let textLabel = sender.titleLabel?.text if let text = textLabel { switch text { case "+", "x", "/", "-","%": if hasFinalString { return } finalString = text isFirstNumber = false hasFinalString = true safeToAdd = true LBLOutput.text = "\(currentText) \(finalString) " break case "=": if !hasFinalString {break} isFirstNumber = true hasFinalString = false isClear = true var result = calculate() if result.truncatingRemainder(dividingBy:1) == 0 { result = (result) } LBLOutput.text = "\(result)" num1 = "\(result)" safeToAdd = false hasSecondDecimal = false hasDecimal = false break case ".": if !safeToAdd{ num1 = "" LBLOutput.text = "" num1 = "\(num1)\(text)" LBLOutput.text = LBLOutput.text! + num1 safeToAdd = true } else if isFirstNumber{ if hasDecimal {break} LBLOutput.text = "" num1 = "\(num1)\(text)" LBLOutput.text = LBLOutput.text! + num1 hasDecimal = true }else{ if hasSecondDecimal {break} num2 = "\(num2)\(text)" LBLOutput.text = "\(currentText)\(text)" hasSecondDecimal = true } break default: if !safeToAdd{ num1 = "" LBLOutput.text = "" num1 = "\(num1)\(text)" LBLOutput.text = LBLOutput.text! + num1 safeToAdd = true } else if isFirstNumber{ LBLOutput.text = "" num1 = "\(num1)\(text)" LBLOutput.text = LBLOutput.text! + num1 }else{ num2 = "\(num2)\(text)" LBLOutput.text = "\(currentText)\(text)" } break; } } } func calculate() -> Double { let firstNumber = Double(num1)! let secondNumber = Double(num2)! num1 = "" num2 = "" isClear = false switch finalString { case "+": return firstNumber + secondNumber case "-": return firstNumber - secondNumber case "x": return firstNumber * secondNumber case "/": return firstNumber / secondNumber case "%": return (firstNumber * 100) / secondNumber default: return 0 } } @IBAction func clearTheLabel(_ sender: Any) { num1 = "" num2 = "" LBLOutput.text = "0" isClear = true } @IBAction func goNegative(_ sender: Any){ var negativeChecker = Double(num1)! if negativeChecker > 0 { num1 = ("-" + num1) LBLOutput.text = num1 }else{ negativeChecker = (-2 * negativeChecker) + negativeChecker num1 = String(negativeChecker) LBLOutput.text = String(negativeChecker) } } @IBAction func squareRoot(_ sender: Any) { let squareRootX = sqrt(Double(LBLOutput.text!)!) num1 = String(squareRootX) LBLOutput.text = String(squareRootX) } } 

而不是试图从你的“大脑”类中调用LBLOutput.text = x ,只需要让大脑函数返回正确的计算值,然后使用viewController将它设置为标签中的文本。 如果你正在关注“MVC”, 就是viewController的用途。

例如,如果你的Brain类有一个叫sum的函数,你可以像这样设置它:

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

并在你的viewController你可以说LBLOutput.text = "\(Brain.sum(x, y: y)"