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
371
votes
15 answers

Global constants file in Swift

In my Objective-C projects I often use a global constants file to store things like notification names and keys for NSUserDefaults. It looks something like this: @interface GlobalConstants : NSObject extern NSString…
user1028028
  • 6,323
  • 9
  • 34
  • 59
371
votes
21 answers

Read and write a String from text file

I need to read and write data to/from a text file, but I haven't been able to figure out how. I found this sample code in the Swift's iBook, but I still don't know how to write or read data. import Cocoa class DataImporter { /* DataImporter…
Jorge Vega Sánchez
  • 7,430
  • 14
  • 55
  • 77
366
votes
30 answers

How do I add 1 day to an NSDate?

Basically, as the title says. I'm wondering how I could add 1 day to an NSDate. So if it were: 21st February 2011 It would become: 22nd February 2011 Or if it were: 31st December 2011 It would become: 1st January 2012.
Andrew
  • 15,935
  • 28
  • 121
  • 203
365
votes
21 answers

How do I check when a UITextField changes?

I am trying to check when a text field changes, equivalent too the function used for textView - textViewDidChange so far I have done this: func textFieldDidBeginEditing(textField: UITextField) { if self.status.text == "" &&…
user4184036
365
votes
32 answers

What is the difference between `let` and `var` in Swift?

What is the difference between let and var in Apple's Swift language? In my understanding, it is a compiled language but it does not check the type at compile time. It makes me confused. How does the compiler know about the type error? If the…
Edward
  • 4,887
  • 3
  • 17
  • 25
364
votes
34 answers

How do I print the type or class of a variable in Swift?

Is there a way to print the runtime type of a variable in swift? For example: var now = NSDate() var soon = now.dateByAddingTimeInterval(5.0) println("\(now.dynamicType)") // Prints "(Metatype)" println("\(now.dynamicType.description()") //…
Matt Bridges
  • 48,277
  • 7
  • 47
  • 61
362
votes
21 answers

How can I use String substring in Swift 4? 'substring(to:)' is deprecated: Please use String slicing subscript with a 'partial range from' operator

I have the following simple code written in Swift 3: let str = "Hello, playground" let index = str.index(of: ",")! let newStr = str.substring(to: index) From Xcode 9 beta 5, I get the following warning: 'substring(to:)' is deprecated: Please use…
Adrian
  • 19,440
  • 34
  • 112
  • 219
356
votes
49 answers

Removing duplicate elements from an array in Swift

I might have an array that looks like the following: [1, 4, 2, 2, 6, 24, 15, 2, 60, 15, 6] Or, really, any sequence of like-typed portions of data. What I want to do is ensure that there is only one of each identical element. For example, the above…
Altair357
  • 3,571
  • 2
  • 11
  • 4
354
votes
15 answers

After upgrading to Xcode 11.2 from Xcode 11.1, app crashes due to _UITextLayoutView

After upgrading to Xcode 11.2 from Xcode 11.1 my app crashes: *** Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: 'Could not instantiate class named _UITextLayoutView because no class named…
Sudhakar Tharigoppula
  • 2,857
  • 3
  • 16
  • 17
350
votes
22 answers

Safe (bounds-checked) array lookup in Swift, through optional bindings?

If I have an array in Swift, and try to access an index that is out of bounds, there is an unsurprising runtime error: var str = ["Apple", "Banana", "Coconut"] str[0] // "Apple" str[3] // EXC_BAD_INSTRUCTION However, I would have thought with all…
Craig Otis
  • 31,257
  • 32
  • 136
  • 234
350
votes
18 answers

Instantiate and Present a viewController in Swift

Issue I started taking a look on the Swift Programming Language, and somehow I am not able to correctly type the initialization of a UIViewController from a specific UIStoryboard. In Objective-C I simply write: UIStoryboard *storyboard =…
E-Riddie
  • 14,660
  • 7
  • 52
  • 74
347
votes
21 answers

How to detect if app is being built for device or simulator in Swift

In Objective-C we can know if an app is being built for device or simulator using macros: #if TARGET_IPHONE_SIMULATOR // Simulator #else // Device #endif These are compile time macros and not available at runtime. How can I achieve the same…
Rafał Sroka
  • 39,540
  • 23
  • 113
  • 143
345
votes
7 answers

Convert an NSURL to an NSString

I have an app where the user can choose an image either from the built-in app images or from the iphone photo library. I use an object Occasion that has an NSString property to save the imagePath. Now in the case of the built-in app images I do get…
Ali
  • 4,205
  • 4
  • 25
  • 40
343
votes
8 answers

Determining if Swift dictionary contains key and obtaining any of its values

I am currently using the following (clumsy) pieces of code for determining if a (non-empty) Swift dictionary contains a given key and for obtaining one (any) value from the same dictionary. How can one put this more elegantly in Swift? // excerpt…
Drux
  • 11,992
  • 13
  • 66
  • 116
341
votes
42 answers

Move view with keyboard using Swift

I have an app that has a text field on the lower half of the view. This means that when I go to type in the text field the keyboard covers the textfield. How would I go about moving the view upwards while typing so I can see what i'm typing and then…
Alex Catchpole
  • 7,156
  • 6
  • 20
  • 29