Questions tagged [swift]

Swift is a general-purpose programming language developed by Apple Inc first released in 2014 for its platforms and Linux. Swift is open-source. Use the tag only for questions about language features or requiring code in Swift. Use the tags [ios], [ipados], [macos], [watch-os], [tvos], [swiftui], [cocoa-touch], and [cocoa] for (language-agnostic) questions about the platforms or frameworks.

Swift is an application and systems programming language introduced by Apple on June 2, 2014, and distributed as open source. Swift interoperates with Objective-C and Cocoa/Cocoa Touch APIs for Apple's iOS, macOS, watchOS, tvOS, and iPadOS operating systems.


Current stable version is Swift 5.8.0 released on November 1, 2022 (download).

  • Swift is open-source, available on GitHub
  • Swift >=2.2 includes support for Swift on a Linux platform.
  • Swift >=5.3 includes support for Swift on a Windows platform.

Please use the tag for questions relating to OpenStack's object/blob store, and the tag for questions relating to the parallel scripting language.

Swift promises to modernize the developer experience over what Objective-C provides, offering features like:

  • Generics
  • Type safety
  • Type inference
  • Namespaces
  • Improved safety (some protection from overflow, use-before-initialization, etc.)
  • Higher-order functions (map, filter, sort)

among others.

The Swift syntax retains some elements from Objective-C while providing an extensive new vocabulary for its new features.

The language can use existing iOS and macOS frameworks, like Cocoa and Cocoa Touch, and can sit side-by-side with Objective-C in applications. Swift applications compile into standard binaries and can be run on OS X 10.9, iOS 7 (or higher), tvOS and watchOS.


Timeline

  • Swift 2.0 was introduced by Apple at WWDC on 8th June 2015. They added new features like the "Error handling model" with try, throw and catch.

  • Swift 2.2 was introduced with Xcode 7.3 on 21st March 2016. (release notes)

  • Swift 3.0 was introduced by Apple at WWDC on 13th June 2016, with the news that Swift will come to non-Apple platforms (Windows, Linux, Android, Raspberry Pi, and so on). (release notes)

  • Swift 3.1 was introduced with Xcode 8.3 on 27th March 2017. (release notes)

  • Swift 4.0 was introduced by Apple at WWDC on 5th June 2017. (release notes)

  • Swift 4.1 was introduced with Xcode 9.3 on 29th March 2018. (release notes)

  • Swift 4.2 was introduced by Apple at WWDC on 4th June 2018. (release notes)

  • Swift 5.0 was introduced with Xcode 10.2 on 25th March 2019. (release notes)

  • Swift 5.1 was introduced with Xcode 11 on 20th September 2019. (release notes)

  • Swift 5.2 was introduced with Xcode 11.4 on 24th March 2020. (release notes)

  • Swift 5.3 was introduced with Xcode 12 on 16th September 2020. (release notes)

  • Swift 5.4 was introduced with Xcode 12.5 on 26th April 2021. (release notes)

  • Swift 5.5 was introduced with Xcode 13 on 20th September 2021. (release notes)

  • Swift 5.6 was introduced with Xcode 13.3 on 14th March 2022. (release notes)

  • Swift 5.7 was introduced with Xcode 14 on 12th September 2022. (release notes)

  • Swift 5.8 was introduced with Xcode 14.3 on 30th March 2023. (release notes)


Resources

References

Books

Other resources


Related Tags

329921 questions
494
votes
18 answers

What does "Fatal error: Unexpectedly found nil while unwrapping an Optional value" mean?

My Swift program is crashing with EXC_BAD_INSTRUCTION and one of the following similar errors. What does this error mean, and how do I fix it? Fatal error: Unexpectedly found nil while unwrapping an Optional value or Fatal error: Unexpectedly…
jtbandes
  • 115,675
  • 35
  • 233
  • 266
484
votes
24 answers

How does String substring work in Swift

I've been updating some of my old code and answers with Swift 3 but when I got to Swift Strings and Indexing with substrings things got confusing. Specifically I was trying the following: let str = "Hello, playground" let prefixRange =…
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
480
votes
9 answers

How do I write dispatch_after GCD in Swift 3, 4, and 5?

In Swift 2, I was able to use dispatch_after to delay an action using grand central dispatch: var dispatchTime: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(0.1 * Double(NSEC_PER_SEC))) dispatch_after(dispatchTime,…
brandonscript
  • 68,675
  • 32
  • 163
  • 220
479
votes
8 answers

Static vs class functions/variables in Swift classes?

The following code compiles in Swift 1.2: class myClass { static func myMethod1() { } class func myMethod2() { } static var myVar1 = "" } func doSomething() { myClass.myMethod1() myClass.myMethod2() myClass.myVar1 =…
Senseful
  • 86,719
  • 67
  • 308
  • 465
475
votes
18 answers

How do I get a reference to the AppDelegate in Swift?

How do I get a reference to AppDelegate in Swift? Ultimately, I want to use the reference to access the managed object context.
AperioOculus
  • 6,641
  • 4
  • 26
  • 37
463
votes
59 answers

dyld: Library not loaded: @rpath/libswiftCore.dylib

I am trying to run a Swift app on my iPhone 4s. It works fine on the simulator, and my friend can successfully run it on his iPhone 4s. I have iOS 8 and the official release of Xcode 6. I have tried Restarting Xcode, iPhone, computer Cleaning &…
user3751185
  • 4,685
  • 3
  • 13
  • 8
463
votes
25 answers

How can I change image tintColor in iOS and WatchKit

I have an UIImageView called "theImageView", with UIImage in a single color (transparent background) just like the left black heart below. How can I change the tint color of this image programmatically in iOS 7 or above, as per the tint method used…
chewy
  • 8,207
  • 6
  • 42
  • 70
456
votes
17 answers

NSNotificationCenter addObserver in Swift

How do you add an observer in Swift to the default notification center? I'm trying to port this line of code that sends a notification when the battery level changes. [[NSNotificationCenter defaultCenter] addObserver:self…
Berry Blue
  • 15,330
  • 18
  • 62
  • 113
450
votes
32 answers

Precision String Format Specifier In Swift

Below is how I would have previously truncated a float to two decimal places NSLog(@" %.02f %.02f %.02f", r, g, b); I checked the docs and the eBook but haven't been able to figure it out. Thanks!
user3524868
  • 4,513
  • 2
  • 12
  • 4
448
votes
21 answers

How to make HTTP request in Swift?

I read The Programming Language Swift by Apple in iBooks, but cannot figure out how to make an HTTP request (something like cURL) in Swift. Do I need to import Obj-C classes or do I just need to import default libraries? Or is it not possible to…
Dicky Tsang
  • 6,135
  • 4
  • 20
  • 18
442
votes
25 answers

How do I convert a Swift Array to a String?

I know how to programmatically do it, but I'm sure there's a built-in way... Every language I've used has some sort of default textual representation for a collection of objects that it will spit out when you try to concatenate the Array with a…
Troy
  • 21,172
  • 20
  • 74
  • 103
440
votes
40 answers

How to use hex color values

I am trying to use hex color values in Swift, instead of the few standard ones that UIColor allows you to use, but I have no idea how to do it. Example: how would I use #ffffff as a color?
Stephen Fox
  • 14,190
  • 19
  • 48
  • 52
433
votes
9 answers

What is the difference between static func and class func in Swift?

I can see these definitions in the Swift library: extension Bool : BooleanLiteralConvertible { static func convertFromBooleanLiteral(value: Bool) -> Bool } protocol BooleanLiteralConvertible { typealias BooleanLiteralType class func…
Jean-Philippe Pellet
  • 59,296
  • 21
  • 173
  • 234
428
votes
32 answers

Get class name of object as string in Swift

Getting the classname of an object as String using: object_getClassName(myViewController) returns something like this: _TtC5AppName22CalendarViewController I am looking for the pure version: "CalendarViewController". How do I get a cleaned up…
Bernd
  • 11,133
  • 11
  • 65
  • 98
427
votes
19 answers

How does one declare optional methods in a Swift protocol?

Is it possible in Swift? If not then is there a workaround to do it?
Selvin
  • 12,333
  • 17
  • 59
  • 80