"CGFloat does not convert to" UInt8 "and other CGFloat issues with Swift and Xcode 6 beta 4

In case this highlights the issue, here is the source code for Objective-C.

int x = (arc4random()%(int)(self.gameView.bounds.size.width*5)) - (int)self.gameView.bounds.size.width*2; int y = self.gameView.bounds.size.height; drop.center = CGPointMake(x, -y); 

I started with this code. Lines 2 and 3 are fine, I will introduce them for clarity later.

 let x = CGFloat(arc4random_uniform(UInt32(self.gameView.bounds.size.width * 5))) - self.gameView.bounds.size.width * 2 let y = self.gameView.bounds.size.height dropView.center = CGPointMake(x, -y) 

In Xcode 6 beta 3, it was necessary to output the result of arc4random_uniform UInt32 to CGFloat so that minus and multiplication work. This no longer works and the compiler shows an error:

'CGFloat does not convert to' UInt8

The release notes indicate:

"CGFloat is now a separate floating point type that wraps either Float on 32-bit architectures or Double on 64-bit architectures. It provides all the same operations of comparison and arithmetic of Float and Double and can be created using numeric literals. CGFloat isolates your code from situations where your code will be! Great for 32-bit, but doesn’t work when creating for 64-bit, or vice versa. (17224725) "

Am I just doing something wrong with types? I don’t even know how to best describe this problem in order to report an error in Apple for beta 4. Almost every Swift project that I have, does any kind of dot problem or direct manipulation, got into this problem, so I looking for some sanity.

+13
type-conversion operator-overloading xcode swift cs193p
Jul 21 '14 at 19:33
source share
4 answers

Since Swift does not have implicit type conversions, you must specify all type conversions that take place. What makes this case especially tedious is that at the moment Swift does not seem to have direct conversions between CGFloat and types like UInt32 , and you have to go through the intermediate type as you discovered.

After all, two double conversions are needed for arc4random_uniform :

 let bounds = CGRectMake(0.0, 0.0, 500.0, 500.0) var x = CGFloat(UInt(arc4random_uniform(UInt32(UInt(bounds.size.width) * 5)))) x -= bounds.size.width * 2 let center = CGPointMake(x, -bounds.size.height) 
+9
Jul 21 '14 at 21:39
source share

There was the same problem ... try wrapping

 arc4random_uniform 

from

 Int() 

as

 Int(arc4random_uniform) 

this worked for me ... I don’t know why Swift / Xcode has unsigned INT's conversion problems

+2
Jul 21 '14 at 20:58
source share

@Arkku provided the correct solution, so single-line for x is ...

 let x = CGFloat(UInt(arc4random_uniform(UInt32(UInt(self.gameView.bounds.size.width) * 5)))) - self.gameView.bounds.size.width * 2 

Starting with Xcode 6 beta 5, you can still use the intermediate conversion if you want, and your code will continue to work. However, it is no longer needed, so it now works as expected.

 let x = CGFloat(arc4random_uniform(UInt32(self.gameView.bounds.size.width * 5))) - self.gameView.bounds.size.width * 2 

Since the original question only applies to Xcode 6 beta 4, what is the correct way to handle this question? Is there a historical mark? Should it be deleted?

0
Jul 21 '14 at 19:33
source share

TL; simple DR calls causing HCF errors: Halt and Catch Fire

Please note that there are some obvious work-related jobs, such as implementing a conversion to and from CGFloat:

Totally legal, but don't do this:

 extension Float { func __conversion() -> CGFloat { return CGFloat(self) } } extension CGFloat { func __conversion() -> Float { return Float(self) } func __conversion() -> Double { return Double(self) } } extension Double { func __conversion() -> CGFloat { return CGFloat(self) } } 

I didn’t notice when typing, but later my car overheated and hung, and SourceKit went 300-500%, and fast swceess + kernel_task took more than 10 gigabytes of RAM, consuming everything that was left of my 16 concerts, It took a lot of time to track it before that - it wasn’t fast.

0
Jul 23 '14 at 3:41
source share



All Articles