What’s New in Swift 2.2?

What’s New in Swift 2.2?

Swift 2.2 is the first official release of this programming language since the time it was made open source. On one hand, there are a few updates interesting to users. On the other hand, the creators decided to withdraw some previously used solutions. Here are some of the most important changes introduced in the new version of Swift.

This time the decision was made to introduce amendments authored by 212 contributors unrelated to Apple. Swift 2.2 is to be a transitional stage before the release of version 3.0.

Selectors

So far, as we could pass as a selector a name of a method in the form of a string. In Swift 2.2 there is no such possibility. There was, however, introduced a new construction – #selector, which checks during compilation whether a particular method exists.

func noMoreStringSelector() {
	
}
 
func selectorTest() {
	let button = UIButton()
	// Swift 2.2
	button.addTarget(nil, action: #selector(noMoreStringSelector), forControlEvents: .AllEvents)
	
	// Before Swift 2.2
	button.addTarget(nil, action: "noMoreStringSelector", forControlEvents: .AllEvents)
}

Version verification

The creators decided also to introduce new, interesting construction which enables checking the version of Swift. This solution will certainly be appreciated by authors of libraries that must support several versions of the language.

#if swift(>=3.0)
	print("Swift 3.0 or later")
#else
	print("Swift 2.2 or earlier")
#endif

C style for loop is deprecated

Until now, we could use a standard C style construction for loop. However, with the new version of Swift, its creators decided to withdraw this option.

//Standard C-style for loop
for var i = 1; i <= 10; i+=1 {
    print(i)
}
//Modern Swift for loop, which may be used instead of
for i in 1...10 {
    print(i)
}

Operators ++ and — are deprecated

At first, this decision can slightly surprise us. The withdrawal of these operators is a consequence of the liquidation of the C style for loop, in which they were mainly used. Of course, if you still need incrementation, you can still use a substitute construction += 1.

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

The following code will stop working with version 3.0:

i++ 
i-- 
++i 
--i 
i = i++

Comparing tuples

Swift 2.2 allows us to compare tuples containing up to 6 elements. It is a simple but useful functionality.

let firstTuple = (1, 2, 3, "string")
let secondTuple = (1, 2, 3, "string")
if firstTuple == secondTuple {
    print("Tuples are equal")
}

More keywords that can be used as argument labels of functions

So far, we could use keywords as argument labels only if we saved them as such (the example uses repeat keyword):

func printName(name: String, `repeat` repeatCount: Int) {
    for _ in 0 ..< repeatCount {
        print(name)
    }
}

It is no longer necessary:

func printName(name: String, repeat repeatCount: Int) {
    for _ in 0 ..< repeatCount {
        print(name)
    }
}

printName("Holdapp", repeat: 5)

Adding removeFirst()

We might have had some problems with removing the first element of an array. In Swift 2.2, thanks to the removeFirst() method, we can now remove one or more first elements.

var elements = [1, 2, 3, 4, 5, 6]
elements.removeFirst()
print(elements) // [2, 3, 4, 5, 6]
elements.removeFirst(3)
print(elements) // [5, 6]

Most of the innovations introduced in Swift 2.2 will certainly be approved by its users. Of course, some changes seem controversial, but it has to be admitted that their implementation is fully justified.

Swift is growing very dynamically, so anyone who wants to keep up with this programming language should regularly follow Swift Evolution and the official changelog.

Learn 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

Promises on the Example of PromiseKit in Objective-C

Sooner or later every programmer encounters the problem of synchronous execution of certain actions. For example: get user information from the API, parse server response, save data to the database, update the view, and many others. To make it even more, at some of these stages you still need to deal with error handling. What should you do exactly?

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 2021

Winning app in
EVERYDAY LIFE

Nagroda Legalnych Bukmacherów

Legal Bookmakers Award 2019

Best Mobile App

Mobile Trends Awards logo

Mobile Trends Awards 2023

Winning app in MCOMMERCE DEVELOPMENT

23

client reviews

Clutch logo