Swift操作符“*”在两个Ints上抛出错误

在这里一个非常奇怪的错误,我搜索了周围,我已经尝试了所有的建议.没有工作
scrollView.contentSize.height = 325 * globals.defaults.integer(forKey: "numCards")

Binary operator ‘*’ cannot be applied to two ‘Int’ operands

WTF Swift!为什么不?我一直乘以Ints.这些是两个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...

所有这些“尝试”似乎都是浪费时间,因为我知道这两个都是Ints …而且我是正确的.请指教.谢谢!

错误是误导性的.该问题实际上是试图将一个Int值分配给一个CGFloat变量.

这将工作:

scrollView.contentSize.height = CGFloat(325 * globals.defaults.integer(forKey: "numCards"))

导致误导性错误的原因(感谢Daniel Hall在下面的评论中)是由于编译器选择返回CGFloat的*函数,因为需要返回值.这个相同的功能需要两个CGFloat参数.由于提供的两个参数是Int而不是CGFloat,所以编译器提供了误导性的错误

Binary operator ‘*’ cannot be applied to two ‘Int’ operands

如果错误更像:

Binary operator ‘*’ cannot be applied to two ‘Int’ operands. Expecting two ‘CGFloat’ operands.

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...