我在这里有一个非常奇怪的错误,我四处搜索,我已经尝试了所有的建议.没有工作.
scrollView.contentSize.height = 325 * globals.defaults.integer(forKey: "numCards")
Run Code Online (Sandbox Code Playgroud)
二进制运算符'*'不能应用于两个'Int'操作数
WTF斯威夫特!为什么不?我Ints一直在增加.这些ARE 2 Ints. globals.defaults只是一个例子UserDefaults.standard.我每次都尝试以下相同的错误.
325 * Int(globals.defaults.integer(forKey: "numCards") //NOPE
Int(325) * Int(globals.defaults.integer(forKey: "numCards")) //NOPE
if let h = globals.defaults.integer(forKey: "numCards"){
325 * h //NOPE, and 'Initializer for conditional binding must have optional type, not Int'
}
let h = globals.defaults.integer(forKey: "numCards") as! Int
325 * h //NOPE, and 'Forced cast of Int of same type as no affect'
325 * 2 //YES! But no shit...
Run Code Online (Sandbox Code Playgroud)
所有这些"尝试"似乎都是浪费时间,因为我知道这些都是Ints......而且我是正确的.请指教.谢谢!
rma*_*ddy 30
该错误具有误导性.这个问题实际上是一个分配的尝试Int值的CGFloat变量.
这将有效:
scrollView.contentSize.height = CGFloat(325 * globals.defaults.integer(forKey: "numCards"))
Run Code Online (Sandbox Code Playgroud)
误导性错误的原因(感谢Daniel Hall在下面的评论中)是由于编译器选择*了CGFloat由于所需返回值而返回的函数.这个功能需要两个CGFloat参数.由于提供的两个参数Int不是CGFloat,编译器会提供误导性错误:
二进制运算符'*'不能应用于两个'Int'操作数
如果错误更像是这样会很好:
二进制运算符'*'不能应用于两个'Int'操作数.期待两个'CGFloat'操作数.
| 归档时间: |
|
| 查看次数: |
4603 次 |
| 最近记录: |