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.


  • 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)




Other resources

Related Tags

329921 questions
44 answers

Passing data between view controllers

I'm new to iOS and Objective-C and the whole MVC paradigm and I'm stuck with the following: I have a view that acts as a data entry form and I want to give the user the option to select multiple products. The products are listed on another view with…
Matt Price
  • 34,499
  • 6
  • 24
  • 33
17 answers

How do I call Objective-C code from Swift?

In Swift, how does one call Objective-C code? Apple mentioned that they could co-exist in one application, but does this mean that one could technically re-use old classes made in Objective-C whilst building new classes in Swift?
David Mulder
  • 26,123
  • 9
  • 51
  • 114
60 answers

How to change Status Bar text color in iOS

My application has a dark background, but in iOS 7 the status bar became transparent. So I can't see anything there, only the green battery indicator in the corner. How can I change the status bar text color to white like it is on the home screen?
Oleksandr Veremchuk
  • 10,975
  • 3
  • 14
  • 9
20 answers

#pragma mark in Swift?

In Objective C, I can use #pragma mark to mark sections of my code in the symbol navigator. Since this is a C preprocessor command, it's not available in Swift. Is there a stand-in for this in Swift, or do I have to use ugly comments?
  • 38,684
  • 22
  • 91
  • 128
9 answers

Swift Beta performance: sorting arrays

I was implementing an algorithm in Swift Beta and noticed that the performance was very poor. After digging deeper I realized that one of the bottlenecks was something as simple as sorting arrays. The relevant part is here: let n = 1000000 var x = …
Jukka Suomela
  • 12,070
  • 6
  • 40
  • 46
19 answers

How to iterate a loop with index and element in Swift

Is there a function that I can use to iterate over an array and have both index and element, like Python's enumerate? for index, element in enumerate(list): ...
  • 12,771
  • 5
  • 30
  • 36
19 answers

#ifdef replacement in the Swift language

In C/C++/Objective C you can define a macro using compiler preprocessors. Moreover, you can include/exclude some parts of code using compiler preprocessors. #ifdef DEBUG // Debug-only code #endif Is there a similar solution in Swift?
  • 20,946
  • 12
  • 59
  • 80
41 answers

Get the length of a String

How do you get the length of a String? For example, I have a variable defined like: var test1: String = "Scott" However, I can't seem to find a length method on the string.
Scott Walter
  • 9,426
  • 4
  • 18
  • 23
41 answers

Split a String into an array in Swift?

Say I have a string here: var fullName: String = "First Last" I want to split the string base on white space and assign the values to their respective variables var fullNameArr = // something like: fullName.explode(" ") var firstName: String =…
  • 12,165
  • 10
  • 34
  • 41
18 answers

How do I see which version of Swift I'm using?

I just created a new Swift project within Xcode. I am wondering which version of Swift it's using. How can I see, in Xcode or the terminal, what version of Swift I am using inside my project?
David Snabel
  • 9,801
  • 6
  • 21
  • 29
24 answers

@selector() in Swift?

I'm trying to create an NSTimer in Swift but I'm having some trouble. NSTimer(timeInterval: 1, target: self, selector: test(), userInfo: nil, repeats: true) test() is a function in the same class. I get an error in the editor: Could not find an…
  • 38,684
  • 22
  • 91
  • 128
43 answers

iOS 8 UITableView separator inset 0 not working

I have an app where the UITableView's separator inset is set to custom values - Right 0, Left 0. This works perfectly in iOS 7.x, however in iOS 8.0 I see that the separator inset is set to the default of 15 on the right. Even though in the xib…
  • 10,163
  • 5
  • 18
  • 24
34 answers

iOS app with framework crashed on device, dyld: Library not loaded, Xcode 6 Beta

This crash has been a blocking issue I used the following steps to reproduce the issue: Create a Cocoa Touch Framework project Add a swift file and a class Dog Build a framework for device Create a Single View application in Swift Import framework…
  • 26,121
  • 9
  • 38
  • 41
23 answers

Convert Int to String in Swift

I'm trying to work out how to cast an Int into a String in Swift. I figure out a workaround, using NSNumber but I'd love to figure out how to do it all in Swift. let x : Int = 45 let xNSNumber = x as NSNumber let xString : String =…
Steve Marshall
  • 7,919
  • 4
  • 16
  • 17
20 answers

How to sort an array of custom objects by property value in Swift

Let's say we have a custom class named imageFile and this class contains two properties: class imageFile { var fileName = String() var fileID = Int() } Lots of them are stored in an Array: var images : Array = [] var aImage =…
  • 13,151
  • 9
  • 53
  • 80
2 3
99 100