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
63
votes
8 answers

Create UITableView programmatically in Swift

I try to implement UITableView programmatically without use of xib or Storyboards. This is my code: ViewController.swift import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() …
Ondra
  • 3,100
  • 5
  • 37
  • 44
63
votes
3 answers

Usage of where in if let assignment in Swift

The Swift documentation at page 61 of the Swift manual hints to the possibility of using where to join an optional binding with a regular condition. Yet when I do it I have a warning suggesting me to substitute the where with a comma like in the…
Fabrizio Bartolomucci
  • 4,948
  • 8
  • 43
  • 75
63
votes
10 answers

Swift 3 Core Data Delete Object

Unfortunately the new Core Data semantics make me crazy. My previous question had a clean code that didn't work because of incorrect auto generation of header files. Now I continue my work with deleting objects. My code seems to be very simple:…
user6485277
63
votes
2 answers

How can I disambiguate a type and a module with the same name?

I'm trying to use Károly Lőrentey's B-tree based OrderedSet in a project. However, I'm running into an issue where I can't declare an unqualified OrderedSet because the name conflicts between Foundation's NSOrderedSet (imported as OrderedSet in…
zneak
  • 134,922
  • 42
  • 253
  • 328
63
votes
9 answers

Make part of a UILabel bold in Swift

I have a UILabel I've made programmatically as: var label = UILabel() I've then declared some styling for the label, including a font, such as: label.frame = CGRect(x: 20, y: myHeaderView.frame.height / 2, width: 300, height: 30) label.font =…
Nick89
  • 2,948
  • 10
  • 31
  • 50
63
votes
6 answers

How to convert Swift Bool? -> String?

Given a Bool?, I'd like to be able to do this: let a = BoolToString(optbool) ?? "" which would either give me "true", "false", or "". Is there a built-in for BoolToString?
user1002430
63
votes
9 answers

facebook login issue - canOpenURL: failed for URL: "fbauth2:///" - error: "(null)"

When I click on login with Facebook button, it is opening safari browser and getting closed immediately. Noticed error on the console. App delegate method: func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) ->…
sfbayman
  • 1,067
  • 2
  • 9
  • 22
63
votes
19 answers

How to dismiss keyboard when touching anywhere outside UITextField (in swift)?

I'm working on a project that have a UIViewController, on the view controller there is a UIScrollView and a UITextField on the scrollview. like this: I'm trying to dismiss the keyboard and hide it after typing some text in the textfield and tap…
White Hat
  • 681
  • 1
  • 6
  • 15
63
votes
12 answers

Swift: Get all subviews of a specific type and add to an array

I have a custom class of buttons in a UIView that I'd like to add to an array so that they're easily accessible. Is there a way to get all subviews of a specific class and add it to an array in Swift?
Kenji Crosland
  • 2,914
  • 6
  • 31
  • 40
63
votes
4 answers

Using existing system sounds in iOS App [swift|

Is it possible to use the existing Apple system sounds in my own app? I would like to write a sample app in Swift that does the following steps: Read/Get a list of all available systemSounds on the device (I think they are located in…
TheOnlyXan
  • 631
  • 1
  • 5
  • 3
63
votes
9 answers

Swift turn a country code into a emoji flag via unicode

I'm looking for a quick way to turn something like: let germany = "DE" into let flag = "\u{1f1e9}\u{1f1ea}" ie, what's the mapping of D to 1f1e9 and E to 1f1ea I was looking at .utf8 for the string, but this returns an integer. FWIW my general…
user1971598
63
votes
5 answers

Arrays of Generics in Swift

I've been playing around with arrays of generic classes with different types. It's easiest to explain my problem with some sample code: // Obviously a very pointless protocol... protocol MyProtocol { var value: Self { get } } extension Int :…
ABakerSmith
  • 22,759
  • 9
  • 68
  • 78
63
votes
7 answers

Converting UTC date format to local nsdate

I am getting from my server a string date in UTC time zone and I need to convert it to the local time zone. MY CODE: let utcTime = "2015-04-01T11:42:00.269Z" let dateFormatter = NSDateFormatter() dateFormatter.timeZone = NSTimeZone(name:…
ilan
  • 4,402
  • 6
  • 40
  • 76
63
votes
5 answers

How to create custom view programmatically in swift having controls text field, button etc

I am trying to access the MyCustomView from another class using the following code in ViewController.swift .. var view = MyCustomView(frame: CGRectZero) .. in the viewDidLoad method. The problem is the view does not get initialized in the…
LC 웃
  • 18,888
  • 9
  • 57
  • 72
63
votes
3 answers

Is there a keyboard shortcut in Xcode 6 to run a single current test function under cursor?

Is there a keyboard shortcut in Xcode 6 to run the current test case under cursor? I know I can press Command + u to run all tests. Also, I can tap the test icon located to the left of the test function or tap the icon in 'Test Navigator'. Is there…
Evgenii
  • 36,389
  • 27
  • 134
  • 170