BLACK FRIDAY SALE: Save 50% on all my Swift books and bundles! >>

Interchangeable use of CGFloat and Double types

Available from Swift 5.5

Paul Hudson      @twostraws

SE-0307 introduced a small but important quality of life improvement: Swift is able to implicitly convert between CGFloat and Double in most places where it is needed.

In its simplest form, this means we can add a CGFloat and a Double together to produce a new Double, like this:

import Foundation
let first: CGFloat = 42
let second: Double = 19
let result = first + second
print(result)

Swift implements this by inserting an implicit initializer as needed, and it will always prefer Double if it’s possible. More importantly, none of this is achieved by rewriting existing APIs: technically things like scaleEffect() in SwiftUI still work with CGFloat, but Swift quietly bridges this to Double.

Save 50% in my Black Friday sale.

SAVE 50% To celebrate Black Friday, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Other changes in Swift 5.5…

Download all Swift 5.5 changes as a playground Link to Swift 5.5 changes

Browse changes in all Swift versions

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.