ios - 无法分配类型“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.
}
解决方案
错误很明显。你的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.
}
}
推荐阅读
- ruby-on-rails - 将 ruby 全局版本更改为 2.6.4 后无法运行 rails
- python - Python - 如何在不使用第三方库的情况下读取图像像素?
- javascript - 我可以通过下一次调用将值传递给委托生成器吗?
- python-3.x - 将循环结果保存到变量中?
- php - 如何解决 Laravel “有效载荷无效”?
- node.js - 在后端使用 express 时在 heroku 中部署应用程序时出错
- javascript - 来自缩小文件的全局变量在 js 文件中未定义?
- linux - 在 linux 中生成 Gsettings 架构文件
- apache-calcite - 测试我自己的适配器时如何使用 CalciteAssert?
- adsense - 如何防止 AdSense 广告在同一窗口中加载广告客户网站