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
51
votes
7 answers

Find first element matching condition in Swift array (e.g. EKSource)

I would like to find the first EKSource of type EKSourceType.Local with a "single"-line expression in Swift. Here is what I currently have: let eventSourceForLocal = eventStore.sources[eventStore.sources.map({ $0.sourceType }) …
Drux
  • 11,992
  • 13
  • 66
  • 116
49
votes
8 answers

iOS. How to enable and disable rotation on each UIViewController?

I have an UIViewController, I want to disable or enable rotation of the screen in different scenarios Example: if flag { rotateDevice = false } else { rotateDevice = true } How can I do that?
Bao Tuan Diep
  • 2,221
  • 2
  • 17
  • 23
47
votes
3 answers

Fatal error: use of unimplemented initializer in custom navigationcontroller

I'm creating a custom navigation controller. I have something like this: public class CustomNavigationController: UINavigationController { // MARK: - Life Cycle override init(rootViewController: UIViewController) { …
user1007522
  • 7,858
  • 17
  • 69
  • 113
47
votes
1 answer

Swift 2 Error using mutating function in Protocol extension "Cannot use mutating member on immutable value: 'self' is immutable

Not sure what's going on here, this seems like it should be pretty straight forward. I have a protocol that mutable var, an extension with a mutating function. Things are crapping out in the testClass.testFunc, when I try and use mtkAnimQueAppend…
47
votes
3 answers

Swift: Extra argument 'error' in call

I'm currently developing my first iOS app using Swift 2.0 and Xcode Beta 2. It reads an external JSON and generates a list in a table view with the data. However, I'm getting a strange little error that I can't seem to fix: Extra argument 'error'…
kaanmijo
  • 713
  • 1
  • 5
  • 14
46
votes
3 answers

Nil is not compatible with expected argument type UIViewAnimationOptions

I just started programming and following a tutorial online I was unable to create this animation. Can anyone tell me why it's saying: Nil is not compatible with expected argument type UIViewAnimationOptions and how to fix it? …
Brenner
  • 655
  • 1
  • 6
  • 20
46
votes
5 answers

When should I compare an optional value to nil?

Quite often, you need to write code such as the following: if someOptional != nil { // do something with the unwrapped someOptional e.g. someFunction(someOptional!) } This seems a bit verbose, and also I hear that using the ! force…
Airspeed Velocity
  • 40,491
  • 8
  • 113
  • 118
45
votes
2 answers

Extension of a nested type in Swift

I have a main class, also providing a namespace: class A { } and a nested class added via an extension (all for the sake of using separate files): extension A { class B { } } I want to add functionality to the nested class (B) by extending it;…
Baglan
  • 1,057
  • 10
  • 23
45
votes
4 answers

Iterate through a String Swift 2.0

I am trying to do a very simple piece of code in Swift playgrounds. var word = "Zebra" for i in word { print(i) } However, I always get an error on line 3. 'String' does not have a member named 'Generator' Any ideas on why this doesn't work?…
Aaron
  • 1,312
  • 3
  • 14
  • 25
44
votes
4 answers

Swift: How to get everything after a certain set of characters

Given the following string: var snippet = "1111 West Main Street Beverly Hills, CA 90210 Phone: 123.456.7891" How can I extract everything that comes after "Phone:" So in this case I want phone = 123.456.7891 I've tried this: if let range =…
user1625155
  • 501
  • 1
  • 8
  • 15
44
votes
3 answers

Why was UITextField's text property changed to an optional in Swift 2?

According to the UIKit diff document, in ios9/Swift 2 var text: String! has become var text: String? According to the documentation for UITextField it specficially says This string is @"" by default. I don't understand the purpose of this change.…
Will
  • 2,604
  • 2
  • 18
  • 14
43
votes
5 answers

Swift 2 internal vs private

I'm confused about the internal and private access modifier. The docs says: “Internal access enables entities to be used within any source file from their defining module, but not in any source file outside of that module. You typically use…
user1007522
  • 7,858
  • 17
  • 69
  • 113
42
votes
2 answers

Trailing where clause for extension of non-generic type

I have the following code: func registerNotification(name:String, selector:Selector) { NSNotificationCenter.defaultCenter().addObserver(self, selector: selector, name: name, object: nil) } func registerKeyboardNotifications() { let…
Sunkas
  • 9,542
  • 6
  • 62
  • 102
42
votes
5 answers

How do you enumerate OptionSetType in Swift?

I have a custom OptionSetType struct in Swift. How can I enumerate all values of an instance? This is my OptionSetType: struct WeekdaySet: OptionSetType { let rawValue: UInt8 init(rawValue: UInt8) { self.rawValue = rawValue } …
Florian
  • 5,326
  • 4
  • 26
  • 35
42
votes
2 answers

Override func error in Swift 2

This code in XCode 6 does not have error but in XCode 7 (Swift 2) this error has occurred : Method does not override any method from its superclass override func touchesBegan(touches: Set, withEvent event: UIEvent) { /* Called…
nmokkary
  • 1,219
  • 3
  • 14
  • 24