Questions tagged [swift2]

Use this tag only for questions directly related to changes in version 2.x of Apple's Swift programming language. Use the tag [swift] for more general language questions, or the tags [ios], [cocoa], [apple-watch] etc for questions about developing on Apple platforms.

Swift 2 is the newest major version of the Swift programming language by Apple.

This version of Swift adds syntax improvements such as the new keywords guard and defer, and also adds error handling with do-catch statements and the try keyword. and Mutability warnings in Swift 2, you'll get warnings in your code whenever you declare variables that never change as constants (using let) rather than variables (using var).

It was made open source on December 3rd of 2015. The source code can be found on github.

Swift was introduced at Apple's 2014 Worldwide Developers Conference (WWDC). It underwent an upgrade to version 1.2 during 2014 and a more major upgrade to Swift 2 at WWDC 2015. Initially a proprietary language, version 2.2 was made open source and made available under the Apache License 2.0 on December 3, 2015, for Apple's platforms and Linux IBM announced its Swift Sandbox website, which allows developers to write Swift code in one pane and display output in another.

The stable Swift version is Swift 2.2.1 which was released on May 3 2016.

As a result of cooperation with Apple, there is an IBM Swift Sandbox for latest Swift syntax.

8879 questions
96
votes
2 answers

How to print details of a 'catch all' exception in Swift?

I'm updating my code to use Swift, and I'm wondering how to print error details for an exception that matches the 'catch all' clause. I've slightly modified the example from this Swift Language Guide Page to illustrate my point: do { try…
markdb314
  • 5,735
  • 6
  • 25
  • 26
92
votes
6 answers

Calling protocol default implementation from regular method

I'm wondering if it's possible to achieve such a thing. I have a Playground like this: protocol Foo { func testPrint() } extension Foo { func testPrint() { print("Protocol extension call") } } struct Bar: Foo { func…
cojoj
  • 6,405
  • 4
  • 30
  • 52
90
votes
18 answers

Framework not found GoogleToolboxForMac

After I update my Firebase via "pod update", I got error like this : ld: warning: directory not found for option…
bennysantoso
  • 1,213
  • 2
  • 12
  • 19
88
votes
8 answers

How can I build a URL with query parameters containing multiple values for the same key in Swift?

I am using AFNetworking in my iOS app and for all the GET requests it makes, I build the url from a base URL and than add parameters using NSDictionary Key-Value pairs. The problem is that I need same key for different values. Here is an example of…
AbhishekDwivedi
  • 883
  • 1
  • 7
  • 5
86
votes
8 answers

When to use inout parameters?

When passing a class or primitive type into a function, any change made in the function to the parameter will be reflected outside of the class. This is basically the same thing an inout parameter is supposed to do. What is a good use case for an…
4thSpace
  • 43,672
  • 97
  • 296
  • 475
77
votes
3 answers

How do I declare that a computed property 'throws' in Swift?

class SomeClass { var someProperty: Int { throw Err("SNAFU") } } For code like the above, the swift binary complains 'error is not handled because the enclosing function is not declared 'throws'. How do I declare that 'someProperty'…
math4tots
  • 8,540
  • 14
  • 58
  • 95
77
votes
8 answers

Extend array types using where clause in Swift

I'd like to use the Accelerate framework to extend [Float] and [Double] but each of these requires a different implementation. I tried the obvious: extension Array { } and get this error: "Constrained extension must be declared on the…
GScrivs
  • 865
  • 1
  • 7
  • 19
74
votes
1 answer

Implicitly lazy static members in Swift

I just noticed that static members of Swift structs are implicitly lazy. For instance, this will only call the init once: class Baz { init(){ print("initializing a Baz") } } struct Foo { static let bar = Baz() } var z =…
cfischer
  • 24,452
  • 37
  • 131
  • 214
73
votes
10 answers

How do I check in Swift if two arrays contain the same elements regardless of the order in which those elements appear in?

Let's say there are two arrays... var array1 = ["a", "b", "c"] var array2 = ["b", "c", "a"] I'd like the result of the comparison of these two arrays to be true, and the following... var array1 = ["a", "b", "c"] var array2 = ["b", "c", "a",…
72
votes
5 answers

Use Swift 2.2 in Xcode 8?

Is it possible to use Swift 2.2 in Xcode 8? From Xcode 8 release notes: "Xcode 8 supports switching toolchains, such as those from swift.org, without relaunching Xcode. (23135507)" I have been trying to find swift 2.2 toolchain on swift.org…
salabaha
  • 2,468
  • 1
  • 17
  • 18
70
votes
4 answers

How to define initializers in a protocol extension?

protocol Car { var wheels : Int { get set} init(wheels: Int) } extension Car { init(wheels: Int) { self.wheels = wheels } } on self.wheels = wheels i get the error Error: variable 'self' passed by reference before being…
bogen
  • 9,954
  • 9
  • 50
  • 89
70
votes
3 answers

Using the split function in Swift 2

Let's say I want to split a string by an empty space. This code snippet works fine in Swift 1.x. It does not work in Swift 2 in Xcode 7 Beta 1. var str = "Hello Bob" var foo = split(str) {$0 == " "} I get the following compiler error: Cannot invoke…
Mallioch
  • 1,416
  • 1
  • 10
  • 17
69
votes
6 answers

How to use stringByAddingPercentEncodingWithAllowedCharacters() for a URL in Swift 2.0

I was using this, in Swift 1.2 let urlwithPercentEscapes = myurlstring.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding) This now gives me a warning asking me to use stringByAddingPercentEncodingWithAllowedCharacters I need to use a…
DogCoffee
  • 19,820
  • 10
  • 87
  • 120
68
votes
13 answers

Ordered map in Swift

Is there any built-in way to create an ordered map in Swift 2? Arrays [T] are sorted by the order that objects are appended to it, but dictionaries [K : V] aren't ordered. For example var myArray: [String] =…
Jojodmo
  • 23,357
  • 13
  • 65
  • 107
66
votes
5 answers

Create NSIndexSet from integer array in Swift

I converted an NSIndexSet to an [Int] array using the answer at https://stackoverflow.com/a/28964059/6481734 I need to do essentially the opposite, turning the same kind of array back into an NSIndexSet.
Jacolack
  • 1,365
  • 2
  • 11
  • 25