Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Double is Not Convertible to CGFloat

I'm trying to draw a simple circle when I get to the following line I get the error "Double is Not Convertable to CGFloat under the startAngle = 0.0

path.addArcWithCenter(center, radius: radius, startAngle: 0.0, endAngle: Float(M_PI) * 2.0, clockwise: true) 

How do I "cast" 0.0 to make it CGFloat in Swift?

The complete function I am writing:

func drawCircle() {     // Drawing code     var bounds:CGRect = secondView.bounds     var center = CGPoint()     center.x = bounds.origin.x + bounds.size.width / 2.0     center.y = bounds.origin.y + bounds.size.height / 2.0     var radius = (min(bounds.size.width, bounds.size.height) / 2.0)     var path:UIBezierPath = UIBezierPath()     path.addArcWithCenter(center, radius: radius, startAngle: CGFloat(0.0), endAngle: Float(M_PI) * 2.0, clockwise: true)     path.stroke()     } 
like image 436
dcbenji Avatar asked Aug 05 '14 02:08

dcbenji


People also ask

What is the difference between the Float double and CGFloat data types Swift?

Suggested approach: It's a question of how many bits are used to store data: Float is always 32-bit, Double is always 64-bit, and CGFloat is either 32-bit or 64-bit depending on the device it runs on, but realistically it's just 64-bit all the time.

What is CGFloat in Swift?

Swift version: 5.6. A CGFloat is a specialized form of Float that holds either 32-bits of data or 64-bits of data depending on the platform. The CG tells you it's part of Core Graphics, and it's found throughout UIKit, Core Graphics, Sprite Kit and many other iOS libraries.


1 Answers

Convert the values that need to be CGFloat to a CGFloat.

path.addArcWithCenter(center, radius: CGFloat(radius), startAngle: CGFloat(0.0), endAngle: CGFloat(M_PI) * 2.0, clockwise: true) 

startAngle probably shouldn't need to be converted though if you're just passing a literal. Also note that this isn't a C style cast, but actually converting between different Swift Types.

Edit: Looking at your whole function, this works.

func drawCircle() {         // Drawing code         var bounds:CGRect = self.view.bounds         var center = CGPoint()         center.x = bounds.origin.x + bounds.size.width / 2.0         center.y = bounds.origin.y + bounds.size.height / 2.0         var radius = (min(bounds.size.width, bounds.size.height) / 2.0)         var path:UIBezierPath = UIBezierPath()         path.addArcWithCenter(center, radius: CGFloat(radius), startAngle: CGFloat(0.0), endAngle: CGFloat(Float(M_PI) * 2.0), clockwise: true)         path.stroke()         } 
like image 140
Connor Avatar answered Sep 21 '22 02:09

Connor