简体   繁体   中英

“'CGFloat' is not convertible to 'UInt8'” - CGFloat wrapping issue

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?

你试一试

self.frame.size.width 

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM