how to convert Int32 value to CGFloat in swift?
Asked Answered
T

2

27

Here my code. I am passing two values into CGRectMake(..) and getting and error.

let width = CMVideoFormatDescriptionGetDimensions(device.activeFormat.formatDescription as CMVideoFormatDescriptionRef!).width
// return Int32 value

let height = CMVideoFormatDescriptionGetDimensions(device.activeFormat.formatDescription as CMVideoFormatDescriptionRef!).height
// return Int32 value

myLayer?.frame = CGRectMake(0, 0, width, height)
// returns error: '`Int32`' not convertible to `CGFloat`

How can I convert Int32 to CGFloat to not return an error?

Torrent answered 27/11, 2014 at 12:36 Comment(1)
Direct Int to CGFloat conversion might be time consuming. Consider using different methods if API is available. For example, you can create CGSize from Int, and then do let dividedSize =size.applying(.init(scaleX: 0.5, y: 0.5)), instead of converting Int's to CGFloat for dividing.Geomorphic
O
72

To convert between numerical data types create a new instance of the target type, passing the source value as parameter. So to convert an Int32 to a CGFloat:

let int: Int32 = 10
let cgfloat = CGFloat(int)

In your case you can either do:

let width = CGFloat(CMVideoFormatDescriptionGetDimensions(device.activeFormat.formatDescription as CMVideoFormatDescriptionRef!).width)
let height = CGFloat(CMVideoFormatDescriptionGetDimensions(device.activeFormat.formatDescription as CMVideoFormatDescriptionRef!).height)

myLayer?.frame = CGRectMake(0, 0, width, height)

or:

let width = CMVideoFormatDescriptionGetDimensions(device.activeFormat.formatDescription as CMVideoFormatDescriptionRef!).width
let height = CMVideoFormatDescriptionGetDimensions(device.activeFormat.formatDescription as CMVideoFormatDescriptionRef!).height

myLayer?.frame = CGRectMake(0, 0, CGFloat(width), CGFloat(height))

Note that there is no implicit or explicit type casting between numeric types in swift, so you have to use the same pattern also for converting a Int to Int32 or to UInt etc.

Omnirange answered 27/11, 2014 at 12:41 Comment(5)
I wonder, is it safe to do this conversion on 32-bit systems, where int32 is (still) 32 bits, however CGFloat is 32 bits too. Taking in account that latter is a floating point and this answer, there will be a loss of precision. Please, correct me if I'm wrong.Constitutionally
I wonder why let x: Int = 1; let y: Int = 2; let progress = CGFloat(x / y) wasn't working for me. However, individually wrapping x and y as CGFloats worked.Heterogynous
@AndrewKirna not working because it doesn't compile, or because of an unexpected result? In the latter case, the result is 0. x and y are Int, the division truncates, so 1 / 2 = 0 However if you convert at least one to CGFloat, the operands are both CGFLoat, so the expression evaluates to 0.5Omnirange
@Antonio, it was because of an unexpected result. But that makes sense...I forgot all Integer division takes the floor of the result even though I'm converting the result to a CGFloat. In other words, the float version of 0 will still be 0.Heterogynous
Expression took 37ms to type-check (limit: 33ms)Nazarite
F
3

Just explicitly convert width and height to CGFloat using the CGFloat's initializer:

myLayer?.frame = CGRectMake(0, 0, CGFloat(width), CGFloat(height))
Farra answered 27/11, 2014 at 12:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.