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
546
votes
34 answers

How do I get the App version and build number using Swift?

I have an IOS app with an Azure back-end, and would like to log certain events, like logins and which versions of the app users are running. How can I return the version and build number using Swift?
Øyvind Vik
  • 5,867
  • 3
  • 14
  • 17
543
votes
35 answers

Rounding a double value to x number of decimal places in swift

Can anyone tell me how to round a double value to x number of decimal places in Swift? I have: var totalWorkTimeInHours = (totalWorkTime/60/60) With totalWorkTime being an NSTimeInterval (double) in second. totalWorkTimeInHours will give me the…
Leighton
  • 6,559
  • 7
  • 21
  • 28
541
votes
23 answers

What does an exclamation mark mean in the Swift language?

The Swift Programming Language guide has the following example: class Person { let name: String init(name: String) { self.name = name } var apartment: Apartment? deinit { println("\(name) is being deinitialized") } } class…
Troy
  • 21,172
  • 20
  • 74
  • 103
535
votes
49 answers

How to hide UINavigationBar 1px bottom line

I have an app that sometimes needs its navigation bar to blend in with the content. Does anyone know how to get rid of or to change color of this annoying little bar? On the image below situation i have - i'm talking about this 1px height line…
Szymon Kuczur
  • 5,783
  • 3
  • 16
  • 14
534
votes
32 answers

How to determine the current iPhone/device model?

Is there a way to get the device model name (iPhone 4S, iPhone 5, iPhone 5S, etc) in Swift? I know there is a property named UIDevice.currentDevice().model but it only returns device type (iPod touch, iPhone, iPad, iPhone Simulator, etc). I also…
The Mach System
  • 6,703
  • 3
  • 16
  • 20
531
votes
26 answers

How to generate a random number in Swift?

I realize the Swift book provided an implementation of a random number generator. Is the best practice to copy and paste this implementation? Or is there a library that does this that we can use now?
door_number_three
  • 5,843
  • 2
  • 16
  • 21
528
votes
23 answers

How to find index of list item in Swift?

I am trying to find an item index by searching a list. Does anybody know how to do that? I see there is list.StartIndex and list.EndIndex but I want something like python's list.index("text").
Chéyo
  • 9,107
  • 9
  • 27
  • 44
525
votes
82 answers

Getting error "No such module" using Xcode, but the framework is there

I'm currently coding in Swift, and I've got an error: No such module Social But I don't understand, because the module is in my project, declared in "Linked frameworks and Libraries" and in "Embedded Binaries". The frameworks is in Objective-C, so…
alexandresecanove
  • 5,725
  • 4
  • 15
  • 14
514
votes
39 answers

Loading/Downloading image from URL on Swift

I'd like to load an image from a URL in my application, so I first tried with Objective-C and it worked, however, with Swift, I've a compilation error: 'imageWithData' is unavailable: use object construction 'UIImage(data:)' My function:…
QuentR
  • 5,227
  • 3
  • 13
  • 12
513
votes
10 answers

Why create "Implicitly Unwrapped Optionals", since that implies you know there's a value?

Why would you create a "Implicitly Unwrapped Optional" vs creating just a regular variable or constant? If you know that it can be successfully unwrapped then why create an optional in the first place? For example, why is this: let someString:…
Johnston
  • 20,196
  • 18
  • 72
  • 121
511
votes
48 answers

Get nth character of a string in Swift

How can I get the nth character of a string? I tried bracket([]) accessor with no luck. var string = "Hello, world!" var firstChar = string[0] // Throws error ERROR: 'subscript' is unavailable: cannot subscript String with an Int, see the…
Mohsen
  • 64,437
  • 34
  • 159
  • 186
505
votes
11 answers

Shall we always use [unowned self] inside closure in Swift

In WWDC 2014 session 403 Intermediate Swift and transcript, there was the following slide The speaker said in that case, if we don't use [unowned self] there, it will be a memory leak. Does it mean we should always use [unowned self] inside…
Jake Lin
  • 11,146
  • 6
  • 29
  • 40
502
votes
43 answers

Close iOS Keyboard by touching anywhere using Swift

I have been looking all over for this but I can't seem to find it. I know how to dismiss the keyboard using Objective-C but I have no idea how to do that using Swift? Does anyone know?
lagoon
  • 6,417
  • 6
  • 23
  • 30
495
votes
12 answers

The use of Swift 3 @objc inference in Swift 4 mode is deprecated?

Briefly, while using Xcode 9 Beta, I have run into the following warning: The use of Swift 3 @objc inference in Swift 4 mode is deprecated. Please address deprecated @objc inference warnings, test your code with “Use of deprecated Swift 3 @objc…
DaleK
  • 5,009
  • 3
  • 10
  • 8
495
votes
15 answers

How do I concatenate or merge arrays in Swift?

If there are two arrays created in swift like this: var a:[CGFloat] = [1, 2, 3] var b:[CGFloat] = [4, 5, 6] How can they be merged to [1, 2, 3, 4, 5, 6]?
Hristo
  • 6,382
  • 4
  • 24
  • 38