Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'var' parameters are deprecated and will be removed in Swift 3 UIimage Gif [duplicate]

Tags:

swift

swift3

I just update Xcode to 7.3 and now I get this warning:

'var' parameters are deprecated and will be removed in Swift 3

I need to use the var in this function:

class func gcdForPair(var a: Int?, var _ b: Int?) -> Int {
    // Check if one of them is nil
    if b == nil || a == nil {
        if b != nil {
            return b!
        } else if a != nil {
            return a!
        } else {
            return 0
        }
    }

    // Swap for modulo
    if a < b {
        let c = a
        a = b
        b = c
    }

    // Get greatest common divisor
    var rest: Int
    while true {
        rest = a! % b!

        if rest == 0 {
            return b! // Found it
        } else {
            a = b
            b = rest
        }
    }
}
like image 362
Ahmed Safadi Avatar asked Dec 11 '25 03:12

Ahmed Safadi


1 Answers

UPDATE: I've reworded my answer because I thought you actually wanted inout, but you don't. So...

The motivation can be found here. The tl;dr is: var gets confused with inout and it doesn't add much value, so get rid of it.

Therefore:

func myFunc(var a: Int) {
    ....
}

Becomes:

func myFunc(a: Int) {
    var a = a
    ....
}

Therefore your code would become:

class func gcdForPair(a: Int?, _ b: Int?) -> Int {
    var a = a
    var b = b
    // Check if one of them is nil
    if b == nil || a == nil {
        if b != nil {
            return b!
        } else if a != nil {
            return a!
        } else {
            return 0
        }
    }

    // Swap for modulo
    if a < b {
        let c = a
        a = b
        b = c
    }

    // Get greatest common divisor
    var rest: Int
    while true {
        rest = a! % b!

        if rest == 0 {
            return b! // Found it
        } else {
            a = b
            b = rest
        }
    }
}
like image 145
Michael Avatar answered Dec 13 '25 22:12

Michael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!