20
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.

Community
  • 1
  • 1
Aeli.li
  • 221
  • 1
  • 2
  • 6

4 Answers4

39

Use CGFloat(number) instead of Float(number)

pipeDown.position = CGPointMake(0.0, CGFloat(posinonY))
MJN
  • 10,748
  • 1
  • 23
  • 32
  • 3
    I'm running into a similar issue, but isn't this done for the developer for 5s? When I select 5s as the simulator, the problem goes away, when I switch back to an iphone 5 simulator, I get "NSNumber not a subtype of CGFloat" – rafalio Jun 11 '14 at 15:40
  • @aleclarson, Xcode could prompt you how to fix it, it's not a shame absolutely – LiangWang Jun 17 '15 at 22:41
  • Just use CGFloats for all such calculations for Core Graphics. Then you do not need the casts either. Do not fight the system. – t1ser May 03 '18 at 17:14
7

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.

Aditya Srivastava
  • 2,630
  • 2
  • 13
  • 23
Seivan
  • 668
  • 6
  • 13
0

As in Swift 4

use

CGPoint(x: 12.0, y: 100.0)
Jack
  • 13,571
  • 6
  • 76
  • 98
0

Use CGFloat(Float). Ex: -- CGFloat(100.0). We can use CGFloat in :

self.collectionHeader.heightAnchor.constraint(equalToConstant: CGFloat(100.0)).isActive = true
Kudos
  • 1,224
  • 9
  • 21