Questions tagged [swift]

Swift is a safe, fast, and expressive 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.4 released on April 26, 2021 (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)


Resources

References

Books

Other resources


Related Tags

296450 questions
1423
votes
45 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
  • 33,201
  • 6
  • 21
  • 33
1046
votes
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
  • 24,033
  • 8
  • 45
  • 104
1031
votes
57 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,435
  • 3
  • 12
  • 9
973
votes
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?
Arbitur
  • 36,899
  • 22
  • 86
  • 123
956
votes
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
  • 11,423
  • 4
  • 32
  • 45
860
votes
16 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): ...
thinker3
  • 11,218
  • 5
  • 26
  • 35
805
votes
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
  • 8,820
  • 4
  • 14
  • 20
800
votes
17 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?
mxg
  • 19,703
  • 12
  • 56
  • 77
738
votes
38 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 =…
blee908
  • 10,735
  • 9
  • 31
  • 39
683
votes
23 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…
Arbitur
  • 36,899
  • 22
  • 86
  • 123
671
votes
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…
user3570727
  • 9,835
  • 4
  • 15
  • 23
644
votes
17 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
  • 8,901
  • 6
  • 19
  • 29
624
votes
19 answers

Do Swift-based applications work on OS X 10.9/iOS 7 and lower?

Will Swift-based applications work on OS X 10.9 (Mavericks)/iOS 7 and lower? For example, I have a machine running OS X 10.8 (Mountain Lion), and I am wondering if an application I write in Swift will run on it. Or what should I have to create a…
MeIr
  • 7,044
  • 6
  • 40
  • 70
609
votes
32 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…
vladof81
  • 24,145
  • 9
  • 35
  • 40
598
votes
22 answers

UITextField text change event

How can I detect any text changes in a textField? The delegate method shouldChangeCharactersInRange works for something, but it did not fulfill my need exactly. Since until it returns YES, the textField texts are not available to other observer…
karim
  • 14,810
  • 7
  • 55
  • 92
1
2 3
99 100