Skip to main content

TupleVariable parameters have been deprecated

About 2 minSwiftArticle(s)bloghackingwithswift.comswiftswift-2.2

TupleVariable parameters have been deprecated 관련

HACKING WITH SWIFT

What's new in Swift?

TupleVariable parameters have been deprecated | Changes in Swift 2.2

TupleVariable parameters have been deprecated

Available from Swift 2.2

Another deprecation, but again with good reason: var parameters are deprecated because they offer only marginal usefulness, and are frequently confused with inout.

To give you an example, here is the printGreeting() function modified to use var:

func printGreeting(var name: String, repeat repeatCount: Int) {
    name = name.uppercaseString

    for _ in 0 ..< repeatCount {
        print(name)
    }
}

printGreeting("Taylor", repeat: 5)

The differences there are in the first two lines: name is now var name, and name gets converted to uppercase so that "TAYLOR" is printed out five times.

Without the var keyword, name would have been a constant and so the uppercaseString line would have failed.

The difference between var and inout is subtle: using var lets you modify a parameter inside the function, whereas inout causes your changes to persist even after the function ends.

As of Swift 2.2, var is deprecated, and it's slated for removal in Swift 3.0. If this is something you were using, just create a variable copy of the parameter inside the method, like this:

func printGreeting(name: String, repeat repeatCount: Int) {
    let upperName = name.uppercaseString

    for _ in 0 ..< repeatCount {
        print(upperName)
    }
}

printGreeting("Taylor", repeat: 5)
Other changes in Swift 2.2…
++ and -- are deprecated | Changes in Swift

++ and -- are deprecated
Traditional C-style for loops are deprecated | Changes in Swift

Traditional C-style for loops are deprecated
Comparing tuples | Changes in Swift

Comparing tuples
Tuple splat syntax is deprecated | Changes in Swift

Tuple splat syntax is deprecated
More keywords can be used as argument labels | Changes in Swift

More keywords can be used as argument labels
Renamed debug identifiers: line, function, file | Changes in Swift

Renamed debug identifiers: line, function, file
Stringified selectors are deprecated | Changes in Swift

Stringified selectors are deprecated
Compile-time Swift version checking | Changes in Swift

Compile-time Swift version checking

Download Swift 2.2 playgroundopen in new window


이찬희 (MarkiiimarK)
Never Stop Learning.