Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 3 mutating functions missing ('...inPlace' methods)

Tags:

swift

swift3

In Swift 2.3, we can write something like this:

var rect = CGRect(...)
rect.offsetInPlace(dx: 15, dy: 0)

to move a rect 15pt to the right.

However in Swift 3, it seems like this function does no longer exist. When inspecting the CGRect interface we can only see the non mutating variant offsetBy(dx:, dy:). This is also true in all the places we've usually used mutating functions (named ...inPlace). I have already searched the Swift evolution repo on GitHub, but couldn't find any notes about this.

Have the mutating variants been removed? Is the remaining function automatically mutating depending on whether or not the return value is used? IMHO, it would be a shame, if they have actually been removed, because they used to be very convenient when doing code based layout, etc.

like image 581
CodingMeSwiftly Avatar asked Sep 11 '16 16:09

CodingMeSwiftly


2 Answers

You can make one for compatibility.

extension CGRect {
    mutating func offsetInPlace(dx: CGFloat, dy: CGFloat) {
        self = self.offsetBy(dx: dx, dy: dy)
    }
}
like image 187
Carter Medlin Avatar answered Nov 21 '22 06:11

Carter Medlin


It seems there is no offsetInPlace anymore. Please see the screen here is all available functions in Instance Methods enter image description here

like image 33
Svitlana Avatar answered Nov 21 '22 05:11

Svitlana