首页 > 解决方案 > 无法分配类型“Int!”的值 键入“字符串”

问题描述

    operation = (sender as AnyObject).tag

我正在开发一个计算器应用程序,而在底部我试图将我的操作发送到 sender.tag 但我收到错误

无法分配类型“Int!”的值 键入“字符串”

This error also generates more errors in the bottom like

“覆盖”只能在类成员上指定”

“‘super’不能在班级成员之外使用”

“'override' 只能在类成员上指定”

“‘super’不能在班级成员之外使用”

这些行产生的错误是什么?

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

标签: iosswift

解决方案


错误很明显。你的operation变量是一个String. 您正在尝试为其分配不兼容的类型Int

let number = (sender as AnyObject).tag
operation = String(number)

或者

operation = "\(number)"

你的班级应该看起来像这样。

class YourClass: UIViewController {
    // ...

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

推荐阅读