Swift operator “*” throwing error on two Ints

后端 未结 1 429
遥遥无期
遥遥无期 2020-12-19 00:17

I have a very odd error here and i\'ve searched all around and i have tried all the suggestions. None work.

scrollView.contentSize.height = 325 * globals.de         


        
相关标签:
1条回答
  • 2020-12-19 00:43

    The error is misleading. The problem is actually the attempt to assign an Int value to a CGFloat variable.

    This will work:

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

    The cause of the misleading error (thanks to Daniel Hall in the comments below) is due to the compiler choosing the * function that returns a CGFloat due to the return value needed. This same function expects two CGFloat parameters. Since the two arguments being provided are Int instead of CGFloat, the compiler provides the misleading error:

    Binary operator '*' cannot be applied to two 'Int' operands

    It would be nice if the error was more like:

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

    0 讨论(0)
提交回复
热议问题