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

Swift Package Manager - UIKit Dependency

I have a Package.swift in my project like: import PackageDescription let package = Package( name: "ProjectName", dependencies: [ .Package(url: "https://github.com/example/repo.git", majorVersion: 0) ] ) When I run…
Onato
  • 9,916
  • 5
  • 46
  • 54
66
votes
5 answers

Swift Protocol Extensions overriding

I'm experimenting with Swift protocol extensions and I found this quite confusing behaviour. Could you help me how to get the result I want? See the comments on the last 4 lines of the code. (You can copy paste it to Xcode7 playground if you want).…
VojtaStavik
  • 2,312
  • 1
  • 18
  • 30
61
votes
4 answers

Is it possible to satisfy Swift protocol and add defaulted arguments?

If you have protocol like so: protocol Messaging { func sendMessage(message: String) } Is there any way to satisfy it in a class like so: class Messager: Messaging { func sendMessage(message: String, count: Int = 1) {} } This would be nice…
Dov
  • 15,530
  • 13
  • 76
  • 177
61
votes
5 answers

.toInt() removed in Swift 2?

I was working on an application that used a text field and translated it into an integer. Previously my code textField.text.toInt() worked. Now Swift declares this as an error and is telling me to do textField.text!.toInt() and it says there is…
Amit Kalra
  • 4,085
  • 6
  • 28
  • 44
58
votes
7 answers

Converting ErrorType to NSError loses associated objects

In Swift 2.0 NSError conforms to the ErrorType protocol. For a customly defined error, we can specify the associating object(s) for some cases, like below. enum LifeError: ErrorType { case BeBorn case LostJob(job: String) case…
Ben Lu
  • 2,982
  • 4
  • 31
  • 54
56
votes
9 answers

Get UIImage only with Kingfisher library

I need to get a UIImage only instead of loading a normal UIImageView with Kingfisher library To realize it I implemented a workaround with UIImageView: let imageView = UIImageView() imageView.kf_setImageWithURL(NSURL(string: cpa.imageName)!,…
Patonz
  • 884
  • 1
  • 6
  • 17
56
votes
5 answers

Swift: How to use for-in loop with an optional?

What's the proper way to use for-in loop with an optional? Right now I always perform an optional binding before looping it. Are there other idioms? let optionalInt:[Int]? = [1, 2, 3] if let optionalInt = optionalInt { for i in optionalInt { …
Boon
  • 40,656
  • 60
  • 209
  • 315
55
votes
9 answers

How to prevent a Command Line Tool from exiting before asynchronous operation completes

In a swift 2 command line tool (main.swift), I have the following: import Foundation print("yay") var request = HTTPTask() request.GET("http://www.stackoverflow.com", parameters: nil, completionHandler: {(response: HTTPResponse) in if let err =…
codecowboy
  • 9,835
  • 18
  • 79
  • 134
54
votes
5 answers

Swift 2, warning: could not load any Objective-C class information from the dyld shared cache

I have found a few questions regarding this issue, yet none of them were helping with my problem. I am trying to save an object to core data using this code (which worked perfectly fine in Xcode 6 and Simulator...): let fetchRequest =…
zero
  • 745
  • 1
  • 5
  • 12
54
votes
8 answers

Swift 2 / iOS 9 - libz.dylib not found

I'm using some external codes from google in my new Swift 2.0 project that required "libz.dylib" in earlier versions. After updating to the new Xcode / the new SDK. Xcode is now unable to import the libz.dylib and throws some errors Undefined…
Daniel K.
  • 1,189
  • 1
  • 10
  • 26
53
votes
4 answers

'self' used before all stored properties are initialized

I'm working through a learn-swift playground and upgrading it to Swift 2.0 as I learn the language. The following code (which likely worked with prior versions of Swift) now generates two errors: "'self' used before all stored properties are…
Ultrasaurus
  • 3,031
  • 2
  • 33
  • 52
53
votes
16 answers

FBSOpenApplicationErrorDomain error 1

I'm trying to run my iOS app that was migrated from Swift 1.2 to Swift 2.0 in Xcode 7 using iPhone Simulator (any), I'm getting the following error: The operation couldn’t be completed. (FBSOpenApplicationErrorDomain error 1.) I already tried to…
Douglas Ferreira
  • 2,279
  • 2
  • 27
  • 42
52
votes
5 answers

Resolving 'Failed to call designated initializer on NSManagedObject class'

I'm new to Swift and I'm trying to learn how to use Core Data. But I'm getting this error and I'm not sure what I've done wrong. I've searched online and tried a few things but I can't get it right. Failed to call designated initializer on…
Daniel
  • 1,399
  • 3
  • 16
  • 29
52
votes
7 answers

extension of Dictionary where

I am trying to create a dictionary extension where Dictionary is of the type . Was looking in many places and trying different approaches, but none of them seemed to work. This was one of them: extension Dictionary where
Andrius Steponavičius
  • 8,074
  • 3
  • 22
  • 25
51
votes
2 answers

WKWebView loads HTML content too slowly in iOS 9 using swift

I have been trying to integrate the WKWebView as a subview of another webview in a UIViewController. I've been able to load the content and communicate properly with the swift and JavaScript. However, the loading time of the HTML content without any…
Krish Lakshmanan
  • 760
  • 7
  • 13