Coding in Swift and get the above error...
Is the message masking something else OR can you really not add two CGFloat operands? If not, why (on earth) not?
EDIT
There is nothing special in the code I am trying to do; what is interesting is that the above error message, VERBATIM, is what the Swift assistant compiler tells me (underlining my code with red squiggly lines).
Running Xcode 6.3 (Swift 1.2)
It's absolutely possible, adding two CGFloat variables using the binary operator '+'. What you need to know is the resultant variable is also a CGFloat variable (based on type Inference Principle).
EDIT: By Swift v3.0 convention
I ran into this using this innocent-looking piece of code:
And found the solution by expanding the error in the Issue Navigator:
Looking into it, I think Swift found the
+(lhs: Float, rhs: Float) -> Float
function first based on its return type (ceilf
takes in aFloat
). Obviously, this takesFloat
s, notCGFloat
s, which shines some light on the meaning of the error message. So, to force it to use the right operator, you gotta use a wrapper function that takes either aDouble
or aFloat
(or just aCGFloat
, obviously). So I tried this and the error was solved:Another approach would be to use a function that takes a
Double
orCGFloat
:So the problem is that the compiler prioritizes return types over parameter types. Glad to see this is still happening in Swift 3 ;P
Mainly two possible reasons are responsible to occur such kind of error.
first:
Whenever you try to compare optional type CGFloat variable like
This is responsible for an error so jus make it as
if a! >= b!{}
Second: Whenever you direct use value to get a result at that time such kind of error occure
like
Don't use as above.
use with object which is declare as a CGFloat
like