gpt4 book ai didi

ios - 将代码从 View Controller 中取出并将其移动到执行所有 Swift 功能的 "Brain"

转载 作者:行者123 更新时间:2023-11-28 12:28:17 24 4
gpt4 key购买 nike

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

是否可以将我的每个函数中的当前代码放入一个 swift 类中来执行它们?

然后我可以在 View Controller 中的每个函数下实现 class.function()。

我想这样做的原因是为了将来我可以只上一个计算器类(class),我也会知道如何在未来的每个项目中做到这一点,我也将能够使用这些类(class)。

我尝试过试验,但我似乎无法在我的其他文件中引用 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)"

关于ios - 将代码从 View Controller 中取出并将其移动到执行所有 Swift 功能的 "Brain",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42775581/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com