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
62
votes
5 answers

How do I declare a class level function in Swift?

I can't seem to find it in the docs, and I'm wondering if it exists in native Swift. For example, I can call a class level function on an NSTimer like so: NSTimer.scheduledTimerWithTimeInterval(0.2, target: self, selector: "someSelector:",…
Logan
  • 52,262
  • 20
  • 99
  • 128
62
votes
2 answers

What is the equivalent of an Objective-C id in Swift?

I'm trying to use an @IBAction to tie up a button click event to a Swift method. In Objective-C the parameter type of the IBAction is id. What is the equivalent of id in Swift?
Doug Richardson
  • 10,483
  • 6
  • 51
  • 77
61
votes
8 answers

Generating resource_bundle_accessor, Type 'Bundle' has no member 'module'

Some times Xcode can not determine the module parameter in the Bundle. Type 'Bundle' has no member 'module' My investigations show that SPM generates an extension on the module (some times) for this property automatically in a file called…
Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
61
votes
1 answer

SwiftUI -> Thread 1: Fatal error: No observable object of type MyObject.Type found (EnvironmentObject in sheet)

I'm building an app with SwiftUI. When I was trying to display a sheet (previously Modal), this error message appears: Thread 1: Fatal error: No observable object of type BixiStationCombinedListViewModel.Type found. A View.environmentObject(_:) for…
Daymo502
  • 825
  • 1
  • 6
  • 10
61
votes
5 answers

@Binding and ForEach in SwiftUI

I can't undertand how to use @Binding in combination with ForEach in SwiftUI. Let's say I want to create a list of Toggles from an array of booleans. struct ContentView: View { @State private var boolArr = [false, false, true, true, false] …
superpuccio
  • 11,674
  • 8
  • 65
  • 93
61
votes
4 answers

How to adjust spacing between HStack elements in SwiftUI?

I have added spacer(minLength: 5) but it takes the minLength. How can I specify the spacing between the text? I have attached a screenshot for reference. I want to reduce the spacing between inner HStack. HStack { …
Let's_Create
  • 2,963
  • 3
  • 14
  • 33
61
votes
6 answers

How to make a Swift enum with associated values equatable

I have an enum of associated values which I would like to make equatable for testing purposes, but do not know how this pattern would work with an enum case with more than one argument. For example, summarised below I know the syntax for making…
user1162328
61
votes
6 answers

Calculating the difference between two dates in Swift

I've seen many approaches how to compute the difference between two dates in terms of a particular date component, e.g. in days, hours, months etc. (see this answer on Stackoverflow): Calendar.current.dateComponents([.hour], from: fromDate, to:…
Mischa
  • 15,816
  • 8
  • 59
  • 117
61
votes
7 answers

What is the difference between final Class and Class?

What is the difference between final Class and Class? final class A { } class B { }
Mohammad Razipour
  • 3,643
  • 3
  • 29
  • 49
61
votes
7 answers

How set swift 3 UITextField border color?

Hello i have working no error codes for UITextfield border color change but when using it in Swift 3 dont change textfield border color and dont gives error. I need your help my codes under below. @IBOutlet weak var email: UITextField! @IBOutlet…
SwiftDeveloper
  • 7,244
  • 14
  • 56
  • 85
61
votes
2 answers

Error when trying to run `pod trunk push [cocoapod].podspec`

When trying to push an update to my cocoapod framework to the repo with pod trunk push as mentioned in the title, I get the following error: [!] Authentication token is invalid or unverified. Either verify it with the email that was sent or…
Justin Vallely
  • 5,932
  • 3
  • 30
  • 44
61
votes
2 answers

How to fix Error: this class is not key value coding-compliant for the key tableView.'

I made an app with Table View and Segmented Control, and this is my first time. I'm using some code and some tutorials, but It's not working. When I run my app It's crashing and it's showing this Error in logs: MyApplication[4928:336085] *…
Emm
  • 1,963
  • 2
  • 20
  • 51
61
votes
4 answers

Is it possible to satisfy Swift protocol and add defaulted arguments?

If you have protocol like so: protocol Messaging { func sendMessage(message: String) } Is there any way to satisfy it in a class like so: class Messager: Messaging { func sendMessage(message: String, count: Int = 1) {} } This would be nice…
Dov
  • 15,530
  • 13
  • 76
  • 177
61
votes
9 answers

iOS Playground doesn't show UI preview

I've created a simple playground with XCode 7.1 and I've typed this simple code: import UIKit import XCPlayground var str = "Hello, playground" let color = UIColor (red: 1 , green: 1 , blue: 0 , alpha: 0 ) let view = UIView()…
Lubbo
  • 1,030
  • 1
  • 10
  • 18
61
votes
12 answers

Check if a string exists in an array case insensitively

Declaration: let listArray = ["kashif"] let word = "kashif" then this contains(listArray, word) Returns true but if declaration is: let word = "Kashif" then it returns false because comparison is case sensitive. How to make this comparison…
Kashif
  • 4,642
  • 7
  • 44
  • 97