How can use CGFloat in Swift?

2019-04-18 22:39发布

var posinonY:Float = Float(y) + Float(pipeDown.size.height) + Float(verticalPipeGap)
pipeDown.position = CGPointMake(0.0, Float(posinonY))

I get this error:

"NSNumber' is not a subtype of 'CGFloat'"

Why?


Edit:

CGFLoat need double type

pipeDown.position = CGPointMake(0.0,  Double(posinonY))

this is ok.

标签: swift cgfloat
3条回答
疯言疯语
2楼-- · 2019-04-18 22:50

As in Swift 4

use

CGPoint(x: 12.0, y: 100.0)
查看更多
ら.Afraid
3楼-- · 2019-04-18 23:03

Use CGFloat(number) instead of Float(number)

pipeDown.position = CGPointMake(0.0, CGFloat(posinonY))
查看更多
Melony?
4楼-- · 2019-04-18 23:16

Since CGFloat is not a Double on 32bit, it becomes hard to use CGGeometry and frameworks like CoreGraphics or SpriteKit. This library makes it a little easier and hopefully Apple takes care of it soon.

I wrote this to help out https://github.com/seivan/ScalarArithmetic

But if you want to just make it work without dependencies use var myFloat:CGFloat explicitly, and make sure to cast any Double (numbers that are type inferred) to CGFloat 2.0 is inferred to be a Double and won't play along with the CG-Structs on 32bit.

查看更多
登录 后发表回答