Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type conversion for NSRect parameters

Am I doing something wrong, or is Swift supposed to be written with constant conversions? For instance, I want to define an NSRect to draw a circle, and I have already defined an NSPoint that will be its centre, also I have a radius of Float type that will define width and height, but it should be multiplied by Int scale before, so I do it like this:

let objectRect = NSRect(x: center.x - radius*scale ...

I get a warning that I cannot multiply Int by Float. Okay, now it looks like this:

let objectRect = NSRect(x: center.x - radius*Float(scale) ...

Now this part is fine, but another warning says that Float cannot be subtracted from CGFloat. Another conversion:

let objectRect = NSRect(x: Float(center.x) - radius*Float(scale) ...

Looks awful already, but to top the cake last warning states that NSRect does not accept Float, so I need to convert it to Double.

like image 505
RomaValcer Avatar asked May 20 '26 02:05

RomaValcer


1 Answers

Do it just once. And as commented below use CGFloat right away:

let objectRect = NSRect(x: CGFloat(center.x) - radius* CGFloat(scale) ...

Coming from scripting languages the Swift type system can be annoying. However, you get used to it.

like image 58
qwerty_so Avatar answered May 23 '26 20:05

qwerty_so



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!