What’s New in Swift 3?

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 modernization 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)

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.

Wojtek

Wojtek Byczkowski

iOS Developer at Holdapp for 5 years. Wojtek believes that less is more in software development and always puts efficiency first when working on projects. Apart from programming, he also has a passion for sports. If you’d like to talk about football or speedway, Wojtek’s definitely your man.

Learn more

Bitrise Tests Made Easier: Update JIRA Issues with Build Number and Forget About Delays in QA Testing

When you trigger Bitrise build with changes and forget to tell QA specialists some essential information, there are two likely scenarios. You waste time waiting for the update from the tester, only you don’t know it’s never coming. Or you get so many questions about builds that you can’t keep up. Sounds familiar? If so, let us tell you about the JIRA issue update that keeps workflow in order.

Read more

How to Send iOS Notifications in Different Languages? Guide to Dynamic Localization

The best way to set a connection between a user, provider, and an app? System notifications. They allow users to get the latest news in no time. This solution is easy to implement in apps dedicated to one market. But it gets complicated when the messages must be displayed in many languages. In such cases, the dynamic localization of remote notifications can be a real game-changer.

Read more

WebSockets on iOS – Real-time Communication That Doesn’t Slow Down the App

When you want to download data for the app, you probably use the API RESTful interface. All it takes is to ask the server for the data and that’s it! This method works well when the app doesn’t need permanent access to new information. But what to do if the content has to be updated in real-time? A delay can make it impossible for the user to buy an item or make a sports bet, for example. But there’s a solution – WebSockets on iOS. Check out why you should implement them, and how to do it.

Read more

Project estimation

Let us know what product you want to build and how we can help you.

Why choose us?

Logo Mobile Trends Awards

Mobile Trends Awards 2017

Nomination in
M-COMMERCE category

Logo Legalni bukmacherzy

Legal Bookmakers Award 2019

Best Mobile App

Mobile Trends Awards logo

Mobile Trends Awards 2020

Nomination in SPORTS & RECREATION category

18

client reviews

Clutch logo