如何使用swift制作货币兑换应用程序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用swift制作货币兑换应用程序相关的知识,希望对你有一定的参考价值。

我需要使用Swift在Xcode中创建一个非常简单的应用程序。该应用程序需要是一个货币兑换应用程序,可以转换4种货币:美元,欧元,比特币和英镑。不需要后端编码。

Code:

import UIKit

class ViewController: UIViewController {
    let rates = [0.9,1.5,1.25,12]
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    @IBOutlet weak var swap: UIButton!
    @IBOutlet weak var reset: UIButton!
    @IBOutlet weak var convertToSegmentControl: UIButton!
    @IBOutlet weak var amountTextField: UITextField!
    @IBOutlet weak var convertedAmountLabel: UITextField!
    @IBOutlet weak var currencySelectorFrom: UISegmentedControl!
    @IBOutlet weak var currencySelectorTo: UISegmentedControl!
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    @IBAction func convertToSegmentControl(_ sender: Any) {
    if let num = Double(amountTextField.text!) {
        switch currencySelectorFrom.selectedSegmentIndex{
        case 0:
            let convertedAmountLabel = rates[0] * num
            let rounded = String(format: "%.2f", convertedAmountLabel)
            convertedAmountLabel.text = rounded
        case 1:
            let convertedAmountLabel = rates[0] * num
            let rounded = String(format: "%.2f", convertedAmountLabel)
            convertedAmountLabel.text = rounded
        case 2:
            let convertedAmountLabel = rates[0] * num
            let rounded = String(format: "%.2f", convertedAmountLabel)
            convertedAmountLabel.text = rounded
        case 3:
            let convertedAmountLabel = rates[0] * num
            let rounded = String(format: "%.2f", convertedAmountLabel)
            convertedAmountLabel.text = rounded
        default:
            break

        }
    }else{
        convertedAmountLabel.text = ""
            print("Could not convert user input to double.")
    }

}

}

Problem:

在第27,31,35和39行,我收到错误。它写道:

'类型'双倍'的值没有成员'文字'。

我该如何解决?

答案

我实际上并不知道Swift,所以请原谅我引入的任何语法错误。

你的整个switch是不必要的。如果你看一下每种情况下的代码,它们都是一样的,不应该是这种情况。

用以下代码替换整个代码:

import UIKit

class ViewController: UIViewController {
    let rates = [0.9,1.5,1.25,12]
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    @IBOutlet weak var swap: UIButton!
    @IBOutlet weak var reset: UIButton!
    @IBOutlet weak var convertToSegmentControl: UIButton!
    @IBOutlet weak var amountTextField: UITextField!
    @IBOutlet weak var convertedAmountLabel: UITextField!
    @IBOutlet weak var currencySelectorFrom: UISegmentedControl!
    @IBOutlet weak var currencySelectorTo: UISegmentedControl!
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    @IBAction func convertToSegmentControl(_ sender: Any) {
    if let num = Double(amountTextField.text!) {
        let cur = currencySelectorFrom.selectedSegmentIndex
        let rounded = String(format: "%.2f", rates[cur] * num)
        convertedAmountLabel.text = rounded

    }else{
        convertedAmountLabel.text = ""
            print("Could not convert user input to double.")
    }

}

}

这是未经测试的,正如我在手机上写的那样,但它应该可行。请注意我如何直接索引rates而不是使用switch

另一答案

看起来你不小心试图将double分配给UITextField

我猜到了你的意图并想出了以下内容......

 import UIKit

class ViewController: UIViewController {
    let rates = [0.9,1.5,1.25,12]
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    @IBOutlet weak var swap: UIButton!
    @IBOutlet weak var reset: UIButton!
    @IBOutlet weak var convertToSegmentControl: UIButton!
    @IBOutlet weak var amountTextField: UITextField!
    @IBOutlet weak var convertedAmountLabel: UITextField!
    @IBOutlet weak var currencySelectorFrom: UISegmentedControl!
    @IBOutlet weak var currencySelectorTo: UISegmentedControl!
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    @IBAction func convertToSegmentControl(_ sender: Any) {
        if let num = Double(amountTextField.text!) {
            switch currencySelectorFrom.selectedSegmentIndex{
            case 0:
            // Renamed this from `convertedAmountLabel` to `convertedAmountValue` (likewise for the other cases)
                let convertedAmountValue = rates[0] * num
            // This line of code consumes the `double` `convertedAmountValue`
                let rounded = String(format: "%.2f", convertedAmountValue)
                convertedAmountLabel.text = rounded
            case 1:
                let convertedAmountValue = rates[0] * num
                let rounded = String(format: "%.2f", convertedAmountValue)
                convertedAmountLabel.text = rounded
            case 2:
                let convertedAmountValue = rates[0] * num
                let rounded = String(format: "%.2f", convertedAmountValue)
                convertedAmountLabel.text = rounded
            case 3:
                let convertedAmountValue = rates[0] * num
                let rounded = String(format: "%.2f", convertedAmountValue)
                convertedAmountLabel.text = rounded
            default:
                break

            }
        }else{
            convertedAmountLabel.text = ""
            print("Could not convert user input to double.")
        }

    }

}

以上是关于如何使用swift制作货币兑换应用程序的主要内容,如果未能解决你的问题,请参考以下文章

行业预测:金融大变革,到时候央行数字货币如何兑换?

重磅!时机成熟后,老百姓如何兑换央行数字货币?

设计一个python程序,计算人民币与其它货币之间汇率的兑换关系

货币兑换英语词汇整理附详解

使用 PayPal iOS sdk 时的货币兑换

如何使用 Swift 使用此代码片段为 iOS 应用程序初始化 SDK?