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
2 answers

How do I quit swift repl without using ctrl-d?

I want to quit swift repl gracefully and not use ctrl-d to exit it. For eg. python repl can be exited by typing exit(). Is there a similar way of quitting swift repl?
Ankit Goel
  • 6,257
  • 4
  • 36
  • 48
62
votes
17 answers

Change the sections header background color in UITableView using an array of headers

I have a array of headers that I use let sectionHeaderTitleArray = ["test1","test2","test3] and they are showed using func tableView[tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return…
Jp4Real
  • 1,982
  • 4
  • 18
  • 33
62
votes
5 answers

Update height constraint programmatically

I am new in auto layout. I have done all of my project from xib file, but now I faced a problem where I have to update an view's height programmatically. I have tried below but now working. [[self view] addConstraint:[NSLayoutConstraint…
Tapas Pal
  • 7,073
  • 8
  • 39
  • 86
62
votes
11 answers

How to make keyboard dismiss when I press out of searchbar on Swift?

I try to make my searchbar on swift, but I have a problem to dismiss keyboard on screen when I pressed out of searchbar. When I try with textfield, it works perfectly fine with this code. override func touchesBegan(touches: NSSet, withEvent event:…
aji
  • 651
  • 1
  • 5
  • 10
62
votes
21 answers

How can I make a countdown with NSTimer?

How can I make a countdown with an NSTimer using Swift?
Giovanie Rodz
  • 1,741
  • 3
  • 12
  • 7
62
votes
7 answers

How to get the RGB Code (INT) from an UIColor in Swift

I would like to get the RGB Value of an UIColor in Swift: let swiftColor = UIColor(red: 1, green: 165/255, blue: 0, alpha: 1) println("RGB Value is:"); println(swiftColor.getRGB()); <<<<<< How to do that ? In Java I would do it as follows: Color…
mcfly soft
  • 11,289
  • 26
  • 98
  • 202
62
votes
3 answers

Synchronizing remote JSON data to local cache storage in iOS Swift

I am trying to find the solution for simple processing all necessary steps for read-only consuming remote JSON data on iOS devices. It means fetching remote JSON data, store to local cache on iOS device for offline usage, refresh the cache, parsing…
kolisko
  • 1,548
  • 3
  • 17
  • 22
62
votes
13 answers

how to check screen size of iphone 4 and iphone 5 programmatically in swift

I need to replace this Objective-C to Swift. Does anyone have any suggestions on how to transfer it? if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { CGSize result = [[UIScreen mainScreen] bounds].size; if(result.height == 480) …
SantoshMaurya
  • 649
  • 1
  • 6
  • 3
62
votes
6 answers

How do you use the Optional variable in a ternary conditional operator?

I want to use an Optional variable with the ternary conditional operator but it is throwing error this error: optional cannot be used as boolean. What am I doing wrong? var str1: String? var myBool:Bool myBool = str1 ? true : false
Yashwanth Reddy
  • 639
  • 1
  • 5
  • 4
62
votes
4 answers

Getting Exception: "Impossible to set up layout with view hierarchy unprepared for constraint"

In my storyboard application I try to add extra UITextField to interface. But I get the exception that title says. I use SwiftAutoLayout library. Here is my code: // MARK: - IBOutlets @IBOutlet weak var passwordTextField: UITextField! // MARK: -…
mustafa
  • 15,254
  • 10
  • 48
  • 57
62
votes
7 answers

Xcode: Any way to refresh/re-run the playground?

The Playground in Xcode automatically updates as you type, but I can't figure out how to get the Playground to "re-compile". In many cases this wouldn't matter, but if you're writing code that generates or uses random values it can be useful to run…
shim
  • 9,289
  • 12
  • 69
  • 108
62
votes
12 answers

Transparent background for WKWebView

In my app I wish to make the WKWebView with transparent background, so the view behind (an ImageView) can show through as background picture. webView!.opaque = false webView!.backgroundColor = UIColor.clearColor() The code above works fine…
Lim Thye Chean
  • 8,704
  • 9
  • 49
  • 88
62
votes
1 answer

Creating UIImage with renderingMode in Swift

In objectiveC I would do this UIImage *image = [[UIImage imageNamed:@"myImage.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; but in Swift I have tried all alternatives like this, without success var image : UIImage =…
Duck
  • 34,902
  • 47
  • 248
  • 470
62
votes
16 answers

How to check if a text field is empty or not in swift

I am working on the code below to check the textField1 and textField2 text fields whether there is any input in them or not. The IF statement is not doing anything when I press the button. @IBOutlet var textField1 : UITextField = UITextField() …
Gokhan Dilek
  • 4,314
  • 4
  • 21
  • 24
62
votes
12 answers

Swift playgrounds with UIImage

I am working with Xcode 6, and I'm trying to recreate the code demoed during session 401 "What's new in Xcode 6". I've added an image to Images.xcassets (called Sample) and within the playground file I'm trying to access this image, as demoed. My…
Ross Gibson
  • 980
  • 1
  • 8
  • 14