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

Draw text along circular path in Swift for iOS

I am looking for some up to date help/hints on how to draw simple single line strings around the edge of a circle using Swift2 for iOS9. I see quite dated examples involving old ObjC fragments, and oft limited to OS X only. Is this even possible in…
Travis Griggs
  • 21,522
  • 19
  • 91
  • 167
41
votes
3 answers

What's wrong here: Instance member cannot be used on type

I have the following code and I'm confused about this error message: Instance member 'mydate' cannot be used on type 'TableViewController' Code: class TableViewController: UITableViewController { let mydate = NSDate() let items = [ …
gurehbgui
  • 14,236
  • 32
  • 106
  • 178
41
votes
3 answers

Swift 2.0: Type of Expression is ambiguous without more context?

The following used to work in Swift 1.2: var recordSettings = [ AVFormatIDKey: kAudioFormatMPEG4AAC, AVEncoderAudioQualityKey : AVAudioQuality.Max.rawValue, AVEncoderBitRateKey : 320000, AVNumberOfChannelsKey: 2, AVSampleRateKey…
lernerbot
  • 903
  • 2
  • 8
  • 21
40
votes
4 answers

Fix warning "C-style for Statement is deprecated" in Swift 3

I have update Xcode to 7.3 and now I have a warning to the function that I use to create random strings. I have tried to change the for statement with for (i in 0 ..< len){...} however, the warning became an error. How can I remove the…
SNos
  • 3,430
  • 5
  • 42
  • 92
40
votes
3 answers

Creating random Bool in Swift

I'm needing to create a random bool value in my game, in Swift. It's basically, if Yes (or 1), spawn one object, if No (or 0), spawn the other. So far, looking at this question and a similar one on here, I found this: let randomSequenceNumber =…
Reanimation
  • 3,151
  • 10
  • 50
  • 88
40
votes
6 answers

Swift debugger does not show variable values when importing ObjC framework

When I create a new OS X "Game" project with Sprite Kit, and set a breakpoint anywhere I can see the variable values just fine: Then I change the code to import my own framework (TilemapKit) which is a pure Objective-C framework: import…
CodeSmile
  • 64,284
  • 20
  • 132
  • 217
40
votes
4 answers

How to extend a protocol that satisfies Multiple Constraints - Swift 2.0

I am trying to provide a default implementation of protocol so it can satisfy multiple constraints from other protocols. Given the following protocols: public protocol Creature { var name: String { get } var canMove: Bool { get } } public…
Audrey Li
  • 980
  • 2
  • 11
  • 14
40
votes
5 answers

Swift 2: Immutable value X is never used consider replacing with '_'

In several cases of my old code I am getting the warning: Immutable value X is never used consider replacing with '_' For instance this often occurs if I am checking an enum which has an associated object (but I don't use the object in this case)…
James Alvarez
  • 7,159
  • 6
  • 31
  • 46
39
votes
4 answers

Detecting available API iOS vs. watchOS in Swift

#available does not seem to work when differentiating between watchOS and iOS. Here is an example of code shared between iOS & watchOS: lazy var session: WCSession = { let session = WCSession.defaultSession() session.delegate = self …
Cezar
  • 690
  • 1
  • 8
  • 17
39
votes
4 answers

Swift 2.0 Sorting Array of Objects by Property

In Swift 2.0, how would you go about sorting an array of custom objects by a property? I know in Swift 1.2, this was done using sorted() and sort(). However, these methods no longer work in Xcode 7 beta 4. Thanks! For example: class MyObject:…
felix_xiao
  • 1,608
  • 5
  • 21
  • 29
39
votes
2 answers

New @convention(c) in Swift 2: How can I use it?

After migrating to Swift 2, I am getting this issue with an error stating that I should now use @convention(c) (T) -> U. I've tried permutations but so far no luck. func foo(context: AnyObject?, width: CGFloat) -> Int { } let bar =…
John Difool
  • 5,572
  • 5
  • 45
  • 80
39
votes
4 answers

swift 2 parse facebookSDK ios9

I am trying to Log In a user via Facebook. In the previous version of swift 1.2 everything worked perfectly, but after migration I can't seem to log in via my FB account I am still getting the error saying: -canOpenURL: failed for URL:…
kalafun
  • 3,512
  • 6
  • 35
  • 49
37
votes
1 answer

How to pass a Swift type as a method argument?

I'd like to do something like this: func doSomething(a: AnyObject, myType: ????) { if let a = a as? myType { //… } } In Objective-C the class of class was Class
juhan_h
  • 3,965
  • 4
  • 29
  • 35
37
votes
7 answers

What is Protocol Oriented Programming in Swift? What added value does it bring?

From Apple's own website: "At the heart of Swift's design are two incredibly powerful ideas: protocol-oriented programming and first class value semantics." Can someone please elaborate what exactly is protocol oriented programming, and what added…
mfaani
  • 33,269
  • 19
  • 164
  • 293
37
votes
4 answers

Return instancetype in Swift

I'm trying to make this extension: extension UIViewController { class func initialize(storyboardName: String, storyboardId: String) -> Self { let storyboad = UIStoryboard(name: storyboardName, bundle: nil) let controller =…
ChikabuZ
  • 10,031
  • 5
  • 63
  • 86