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.

8489 questions
198
votes
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
  • 8,901
  • 6
  • 19
  • 29
194
votes
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 =…
Khawar
  • 8,881
  • 9
  • 41
  • 65
194
votes
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,146
  • 6
  • 35
  • 67
184
votes
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?
Abdurrahman
  • 5,836
  • 3
  • 16
  • 19
170
votes
6 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…
mustafa
  • 14,396
  • 9
  • 42
  • 56
159
votes
10 answers

Simplest way to throw an error/exception with a custom message in Swift 2?

I want to do something in Swift 2 that I'm used to doing in multiple other languages: throw a runtime exception with a custom message. For example (in Java): throw new RuntimeException("A custom message here") I understand that I can throw enum…
markdb314
  • 4,636
  • 5
  • 23
  • 24
156
votes
9 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…
lmiguelvargasf
  • 40,464
  • 32
  • 169
  • 172
142
votes
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,489
  • 8
  • 45
  • 81
140
votes
11 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
  • 13,891
  • 5
  • 52
  • 91
136
votes
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…
Logan
  • 49,229
  • 19
  • 92
  • 123
133
votes
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…
Maysam
  • 6,738
  • 13
  • 63
  • 94
116
votes
4 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
  • 5,557
  • 4
  • 33
  • 43
114
votes
4 answers

How to silence a warning in swift

I have a piece of code which is generating lots of warnings (deprecated API) Using clang* I could do #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" ... #pragma clang diagnostic pop However this does…
Antzi
  • 11,625
  • 6
  • 40
  • 66
112
votes
5 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
  • 2,958
  • 3
  • 16
  • 24
101
votes
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…
user5046034
1
2 3
99 100