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
13 answers

Swift's guard keyword

Swift 2 introduced the guard keyword, which could be used to ensure that various data is configured ready to go. An example I saw on this website demonstrates an submitTapped function: func submitTapped() { guard username.text.characters.count >…
David Snabel
  • 9,801
  • 6
  • 21
  • 29
11 answers

Swift: guard let vs if let

I have been reading about Optionals in Swift, and I have seen examples where if let is used to check if an Optional holds a value, and in case it does – do something with the unwrapped value. However, I have seen that in Swift 2.0 the keyword guard…
  • 63,191
  • 45
  • 217
  • 228
1 answer

try, try! & try? what’s the difference, and when to use each?

In Swift 2.0, Apple introduced a new way to handle errors (do-try-catch). And few days ago in Beta 6 an even newer keyword was introduced (try?). Also, knew that I can use try!. What's the difference between the 3 keywords, and when to use each?
  • 6,306
  • 3
  • 19
  • 20
6 answers

Binary operator '|' cannot be applied to two UIViewAutoresizing operands

Getting this error in Swift 2.0. Binary operator '|' cannot be applied to two UIViewAutoresizing operands Here is the code: let view = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 568)) addSubview(view) view.autoresizingMask =…
  • 9,151
  • 9
  • 46
  • 67
4 answers

Swift 2.0 - Binary Operator "|" cannot be applied to two UIUserNotificationType operands

I am trying to register my application for local notifications this way: UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Alert | UIUserNotificationType.Badge, categories:…
Nikita Zernov
  • 5,465
  • 6
  • 39
  • 70
7 answers

Swift do-try-catch syntax

I give it a try to understand new error handling thing in swift 2. Here is what I did: I first declared an error enum: enum SandwichError: ErrorType { case NotMe case DoItYourself } And then I declared a method that throws an error (not an…
  • 15,254
  • 10
  • 48
  • 57
6 answers

Overriding methods in Swift extensions

I tend to only put the necessities (stored properties, initializers) into my class definitions and move everything else into their own extension, kind of like an extension per logical block that I would group with // MARK: as well. For a UIView…
Christian Schnorr
  • 10,768
  • 8
  • 48
  • 83
6 answers

Protocol can only be used as a generic constraint because it has Self or associatedType requirements

I have a protocol RequestType and it has associatedType Model as below. public protocol RequestType: class { associatedtype Model var path: String { get set } } public extension RequestType { public func…
Rahul Katariya
  • 3,528
  • 3
  • 19
  • 24
12 answers

The "++" and "--" operators have been deprecated Xcode 7.3

I am looking at Xcode 7.3 notes and I notice this issue. The ++ and -- operators have been deprecated Could some one explain why it is deprecated? And am I right that in new version of Xcode now you going to use instead of ++ this x +=…
Oleg Gordiichuk
  • 15,240
  • 7
  • 60
  • 100
5 answers

print without newline in swift

In swift 2.0, print() automatically adds a newline character. In swift 1.2, println() and print() used to be separate functions. So how do I print some text and not add a newline to it since swift no longer has a print function that does not append…
Ankit Goel
  • 6,257
  • 4
  • 36
  • 48
11 answers

stringByAppendingPathComponent is unavailable

My app shares photo on Instagram, to do this it first saves it on a temporary directory: let writePath = NSTemporaryDirectory().stringByAppendingPathComponent("instagram.igo") It was working on Swift 1.2, but does not work on Swift 2.0. Given error…
  • 7,246
  • 13
  • 68
  • 106
12 answers

Get integer value from string in swift

So I can do this: var stringNumb: NSString = "1357" var someNumb: CInt = stringNumb.intValue But I can't find the way to do it w/ a String. I'd like to do something like: var stringNumb: String = "1357" var someNumb: Int = Int(stringNumb) This…
  • 52,262
  • 20
  • 99
  • 128
5 answers

How to remove a key-value pair from swift dictionary?

I want to remove a key-value pair from a dictionary like in the example. var dict: Dictionary = [:] //Assuming dictionary is added some data. var willRemoveKey = "SomeKey" dict.removePair(willRemoveKey) //that's what I need
do it better
  • 4,627
  • 6
  • 25
  • 41
1 answer

Swift Modal View Controller with transparent background

I know this topic is quite popular, but I'm a little iniciate problem in a programming language, the fact is that I still do not understand where I put the code. Well, I'll tell the whole case: I'm trying to make a modal Swift in a little different…
13 answers

How to present view controller from right to left in iOS using Swift

I am using presentViewController to present new screen let dashboardWorkout = DashboardWorkoutViewController() presentViewController(dashboardWorkout, animated: true, completion: nil) This presents new screen from bottom to top but I want it to…
Umair Afzal
  • 4,947
  • 5
  • 25
  • 50
2 3
99 100