Questions tagged [swift5]

Use this tag only for questions directly related to changes in version 5 of Apple's Swift programming language. Use the tag [swift] for more general language questions, or the tags [ios], [cocoa], [apple-watch] etc. for questions about developing on Apple platforms.

Swift 5 is a version of the Swift language developed by Apple and was released March 26th, 2019 with Xcode 10.2. The language is open source and available on GitHub (see ).

Swift 5 follows and .

Swift 5 Release Notes for Xcode 10.2.

3158 questions
33
votes
4 answers

Method 'scene(_:openURLContexts:)' is not called

In the info.plist file I configured URL Identifier and URL Scheme successfully. Also I am able to open the app using the custom URL. The problem is when the app launches for the first time, the method func scene(_ scene: UIScene, openURLContexts…
iPhone
  • 4,092
  • 3
  • 34
  • 58
32
votes
3 answers

Use reference to captured variable in concurrently-executing code

Update 2: I suspect the question gets upvoted because of the possible solution that I describe. Highlighted it for clarity. Update 1: This question gets a lot of views. If you think the question can be enhanced with the situation in which you…
Arjan
  • 16,210
  • 5
  • 30
  • 40
32
votes
5 answers

How can I use async/await with SwiftUI in Swift 5.5?

I have been testing the async/await functionality previewed in the Swift 5.5 release, but I am unable to collect the results from an async function and display them using SwiftUI. Here is my code: import SwiftUI struct AsyncTestView: View { …
mistry
  • 459
  • 1
  • 6
  • 16
32
votes
2 answers

Instantiated optional variable shows as nil in Xcode debugger

Since I upgraded to Xcode 11 and Swift 5.1 I've encountered a strange issue -- after an optional variable is instantiated, it can still show as up nil in the Xcode debugger! I have an optional class variable called booking: var booking:…
Dylan
  • 2,315
  • 2
  • 20
  • 33
31
votes
5 answers

No exact matches in call to instance method error message in Swift

I never get this before, What is the meaning of this error message in Swift: No exact matches in call to instance method 'dataTask(with:completionHandler:)' Here is my code block: var request: NSMutableURLRequest? = nil let task =…
MGY
  • 7,245
  • 5
  • 41
  • 74
31
votes
3 answers

How to check if a view is displayed on the screen? (Swift 5 and SwiftUI)

I have a view like below. I want to find out if it is the view which is displayed on the screen. Is there a function to achieve this? struct TestView: View { var body: some View { Text("Test View") } }
codezero11
  • 427
  • 1
  • 5
  • 10
30
votes
4 answers

How can I wait for an async function from synchronous function in Swift 5.5?

When conforming to protocols or overriding superclass methods, you may not be able to change a method to be async, but you may still want to call some async code. For example, as I am rewriting a program to be written in terms of Swift's new…
deaton.dg
  • 1,282
  • 9
  • 21
29
votes
4 answers

Can a Swift Property Wrapper reference the owner of the property its wrapping?

From within a property wrapper in Swift, can you someone refer back to the instance of the class or struck that owns the property being wrapped? Using self doesn't obviously work, nor does super. I tried to pass in self to the property wrapper's…
kennyc
  • 5,490
  • 5
  • 34
  • 57
26
votes
3 answers

XOR in Swift 5?

I'm trying to do an XOR operation in Swift 5. The documentation does not seem to mention explicitly doing it with two boolean values here: https://docs.swift.org/swift-book/LanguageGuide/AdvancedOperators.html Is this possible? It says to use the ^…
Zack117
  • 985
  • 1
  • 13
  • 28
25
votes
3 answers

How to detect whether targetEnvironment is iPadOS in SwiftUI?

I'd like to display different views when building for iOS and iPadOS. Currently, I know I can do import SwiftUI struct ContentView: View { #if targetEnvironment(macCatalyst) var body: some View { Text("Hello") } #else …
cyril
  • 3,020
  • 6
  • 36
  • 61
25
votes
3 answers

Using SecRandomCopyBytes in Swift

I want to generate random bytes using SecRandomCopyBytes in Swift 3.0. Here is how I did it in Swift 2.2 private static func generateRandomBytes() -> String? { let data = NSMutableData(length: Int(32)) let result =…
hockeybro
  • 981
  • 1
  • 13
  • 41
24
votes
7 answers

Constraints resets when app is going in background - iOS 13

I have set a view's leading, trailing constraints normally. I have set its height to static 325. And for bottom constraint I have set 2 constraints 1. with main view's bottom constraint to view's bottom constraint. 2. with main view's bottom…
Rajat Mishra
  • 995
  • 7
  • 18
23
votes
2 answers

What determines whether a Swift 5.5 Task initializer runs on the main thread?

This is sort of a follow-up to my earlier asyncDetached falling back into main thread after MainActor call. Here's the complete code of an iOS view controller: import UIKit func test1() { print("test1", Thread.isMainThread) // true Task { …
matt
  • 515,959
  • 87
  • 875
  • 1,141
23
votes
2 answers

SwiftUI - is it possible to get didSet to fire when changing a @Published struct?

I have just updated to XCode 11.4 and some of my code has stopped working. I have some @Published struct variables in an ObservableObject. Previously, when I updated properties on the struct, the didSet method would fire on the published property,…
codewithfeeling
  • 6,236
  • 6
  • 41
  • 53
22
votes
2 answers

UIButton title "Button" is not cleared after removing it from the attribute inspector

I cleared the title from attribute inspector, but while running the app, text "Button" is displaying. Output: I checked different scenarios and the issue occurs only if the deployment target is below 15.0 Is there any solution for this?. Xcode…
Akash S Arjun
  • 233
  • 2
  • 5