A lot has changed with the release of Swift 3. The good news is that Swift got better by a lot. The not so good news is that some of your Swift 2 code needs some tweaking. In this post, I show you an example of the type of errors you can expect and how easy it is to fix them.

CGRectMake Is Unavailable in Swift 3

If you are running into this error, then you have likely migrated from Swift 2 to Swift 3. As the error points out, the CGRectMake(_:_:_:_:) isn't available in Swift 3. It has been deprecated in Swift 3. The good news is that creating a CGRect object is much swiftier as of Swift 3. Let me show you how to create a CGRect object in Swift 3 and later.

Understanding the Error

If you're reading this, then you are used to creating a CGRect by invoking the CGRectMake(_:_:_:_:) function. That is no longer possible in Swift 3 and onwards.

let rect = CGRectMake(0.0, 0.0, 100.0, 100.0)

Creating a CGRect the Swift 3+ Way

Instead of using CGRectMake(_:_:_:_:), you make use of the initializer of the CGRect struct.

let rect = CGRect(x: 0.0, y: 0.0, width: 100.0, height: 100.0)

The initializer of the CGRect struct defines the same parameters, x, y, width, and height.

Other Related Changes

Swift 3 deprecates many more functions to create Core Graphics structures. The idea is the same, though. To create a CGPoint object, you no longer invoke CGPointMake(_:_:). You use the structure's initializer instead.

let point = CGPoint(x: 0.0, y: 0.0)

To create a CGSize object, you no longer invoke CGSizeMake(_:_:). You use the structure's initializer instead.

let size = CGSize(width: 20.0, height: 100.0)

What's Next?

Swift has evolved significantly over the years, and some changes were introduced to make the language more consistent and expressive. The transition from CGRectMake(_:_:_:_:) to the CGRect initializer is one such change. I hope you agree that the CGRect initializer makes your code more readable and swiftier.