Swift 5.5 auto conversions between CGFloat and Double

Now, Swift converts CGFloat to double and double to CGFloat implicitly for us.

devCracker
2 min readOct 23, 2021
latest addition in Swift 5.5 to convert CGFloat to Double. Xcode13
Photo by Jean-Philippe Delberghe on Unsplash

Swift 5.5 came up with a lot of cool additions like actors, Task, aync/await etc. Along with that, a small and super useful addition also came in. Yes, Swift 5.5 compiler does the automatic conversions from CGFloat to Double and Vice-versa. This seems a smaller one in mind but super useful in our daily life.

So far(Before Swift 5.5 new addition), We used to get the error when we try to assign the Double value to CGFloat variable or vice versa, we get the compiler error as below.

“Cannot convert value of type ‘CGFloat’ to specified type ‘Double’ ”

Swift CGFloat to double conversion error
CGFloat to Double or Double to CGFloat conversion error.

We have to face this more often to pass the CGFloat value to Double or other-way around in our development life. Isn’t it?

Swift 5.5, Auto conversion from CGFloat to Double or ViceVersa.

Swift 5.5 brings this small and very useful addition which does this conversion for us automatically, so we don’t need to do the type conversion any more. Below screenshot is from XCode13 where we don’t get the conversion error. Yeah super useful tiny addition.

CGFloat to Double or Double to CGFloat auto conversion in Swift. Xcode 13
CGFloat to Double or Double to CGFloat auto conversion in Swift.

Thanks for reading this short one!!!.

Few last words:

My other articles such as Actors in Swift, @MainActor, Thread Sanitiser or TSan, Securing the app content from screenshot or recordings and etc. might be useful to you.

Upgrade your medium membership from here to read good amount of article and scale your skills.

--

--

devCracker

professional mobile app developer | iOS | Android | GraphQL | Typescript | nodejs. Sports and musics fuels my mind to get back to development