11 November

What's New in Swift 3?

Recently, the new 3.0 version of the Swift language has been presented. Some of the noticeable changes include modifications involving the removal of functions which have already been removed in Swift 2.2. Changes involving modernisation of the language are also present.

C-Style For-Loops

Until now, we have been able to use the standard for-loop structure known from the C language. In Swift 3, we are no longer allowed to do that. We can instead use its equivalents: the open and half-open range operators.

for(var i = 0; i < 10; i += 1) {
            print(i)
        }
        for i in 0...9 {
            print(i)
        }

Alternatively we can use a for-each loop.

(0…9).forEach {
            print($0)
 }
The ++ and -- Operators

The incrementation and decrementation operators occur in two types: with a prefix or with a suffix. Removal of those operators is a result of errors occurring when selecting their type, and of the removal of C-style for-loops.

var i = 0
 i++ // Error, XCode offers to use: 
 i += 1

This code doesn't work in version 3.0.

i++
++i
i--
--i
Tuple as a Function Argument List

Until now, it has been possible to use an obscure function to transfer an entire argument list as a single value.

printNames(firstParameter: 7, secondParameter: 8)
let singleValue = (firstParameter: 7, secondParameter: 8)
printNames(singleValue)

The current version no longer allows such practices.

Label for the First Parameter of a Function

In Swift 2 and earlier releases, method names did not require a label for the first parameter.

printNames(8, secondParameter: 7)

In the new version, a label must be specified for the first parameter as well.

printNames(firstParameter: 8, secondParameter: 7)

Apple offers a way to bypass this requirement.

printNames(_ firstParameter: 8, secondParameter: 7)
Var in Function Parameters

In most cases, we do not need to modify a function parameters within it, which is why they are defined as constant. However, there are instances where it helps to do so. In Swift 2, one can use the var keyword for this, which precedes the function parameter name.

func myFunction(var firstValue: Int, var secondValue: Int) -> Int {
firstValue += 1
return firstValue
}

The latest version removes var from the function parameters. Therefore, function parameter values must be assigned to local variables.

func myFunction( firstValue: Int, secondValue: Int) -> Int {
var firstValue = firstValue
	firstValue += 1
	return firstValue
}
Unnecessary Words

The method names which contained obvious words now have those words removed. This makes method names much shorter.

//Swift 2
"Hello World".capitalizedString
let color = UIColor.blackColor
let numbers = [1,2,3,4]
numbers.maxElement()
UIDevice.currentDevice()


//Swift 3
"Hello World".capitalized
let color = UIColor.black
let numbers = [1,2,3,4]
numbers.max()
UIDevice.current
}
Naming Convention

Swift 3 separates methods into two groups: one using nouns for naming (they return a certain value) and another using verbs (they perform certain actions on the elements).

// Enumerate() w Swift 3
var numbers = [17, 26, 1, 36]
for (index, value) in numbers.enumerated() {
print("tab[\(index)] = \(value)")
}

The sort() and sortInPlace() methods are another example. They are now replaced by the sorted() and sort() methods, respectively.

//Swift 2
let newArrayInSwift2 = numbers.sort()
print(newArrayInSwift2)
// Swift 3
let newArrayInSwift3 = numbers.sorted()
print(newArrayInSwift3)
//Swift 2
numbers.sortInPlace()
print(numbers)
//Swift 3
numbers.sort()
print(numbers)
Conclusion

Each new version of Swift makes this language easier to learn, easier to use and faster. Swift 3 contains many fundamental changes which will affect our existing code and help avoid some of the errors in the future.