"'CGFloat' is not convertible to 'UInt8'" - CGFloat wrapping issue

509 views Asked by At
label.position = CGPointMake(self.size.width*0.125, self.size.height-CGFloat(0.15)*self.size.height*_activeEnemiesArray.count)

I get an error pointing to self.size.width that says

'CGFloat' is not convertible to 'Double'.

This makes sense, since a CGFloat value and a Double value cannot be multiplied, but then when I convert the 0.125 to CGFloat:

label.position = CGPointMake(self.size.width*CGFloat(0.125), self.size.height-0.15*self.size.height*_activeEnemiesArray.count)

I get an error pointing to self.size.width that says

'CGFloat' is not convertible to 'UInt8'

This does not make any sense. Has anyone else been experiencing this and what could be the cause?

2

There are 2 answers

0
Nate Cook On BEST ANSWER

Xcode is leading you astray -- literals shouldn't really ever cause problems, as they'll get converted into Double or CGFloat as needed once everything else is the same type. Instead, what's going on is that you have an Int at the very end of your line:

... * self.size.height * _activeEnemiesArray.count)

That count property needs to be converted to CGFloat so it can be used with the rest:

... * self.size.height * CGFloat(_activeEnemiesArray.count))
0
Shanmugasundharam On

just try

self.frame.size.width