Questions tagged [swift-optionals]

56 questions
1
vote
3 answers

How to create a generic function in Swift that will reject the given parameter unless it is an Optional?

This question is a follow up to my earlier question: I expected the system to report non protocol conformance, but it does not! Why? Please read the referred question for you to get a better idea of the constraints at hand. I created a generic…
0
votes
2 answers

Is there a way to embed an assertionFailure into '??' expression?

TimeZone(abbreviation: "GMT+1") ?? TimeZone.current If you want to have a non optional and assert before TimeZone.current fallback is used after ?? what syntax would I use in swift? something like this TimeZone(abbreviation: "GMT+1") ??…
Anton Tropashko
  • 5,486
  • 5
  • 41
  • 66
0
votes
1 answer

Why the Picker is not working? Can't select option when I set the @State is optional

I'm making a picker. When I set the @State length to optional, it's not working. I can't select any option. When I set @State length to a Double, it's working. But it associates with another picker. I want that length can be nothing, when another…
lvyachen
  • 33
  • 3
0
votes
2 answers

Swift WKwebView.evaluateJavaScript Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

I want to execute javascript in an external screen's webview. In my main view I'm trying to call the pop() function in the External View like this: let ex = ExternalDisplayViewController() ex.pop(str: "Hello!") When I run it I get "Fatal error:…
Josiah
  • 1,117
  • 5
  • 22
  • 35
0
votes
1 answer

Getting nil from Enum Swift

I have created a class class NewTabContainerModal { var newContainerDate = Date() var newContainerSelectedFilter : NewAllErrorFIlter = .Yearly func resetModal () { newContainerSelectedFilter = .Yearly newContainerDate = Date() } } I have…
Wings
  • 2,398
  • 23
  • 46
0
votes
0 answers

How do I access data in a nested json in Swift(while using optionals in my data model)

I have my struct as: struct CategoryViewModel2: Codable { let data: [Datum2]? } // MARK: - Datum struct Datum2: Codable { let the0: Int? let the1, the2: String? let the3, the4, id: Int? let name, image: String? let parentID,…
0
votes
0 answers

"Unexpectedly found nil while implicitly unwrapping an Optional value" using the Date() structure, then transitioning to another storyboard

I created a label to show the current date when the view loads, but when I transition to another view, I get the: Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value. The UILAbel works fine and shows the…
Pedro
  • 1
  • 1
0
votes
1 answer

Generic decoding json into object with optionals

I wrote an extension to Decodable with the hopes of having a generic constructor for objects from json strings, it looks like this: extension Decodable { init?(with dictionary: [String: Any]) { guard let data = try?…
Sebby Fay
  • 181
  • 1
  • 5
0
votes
2 answers

How do you assign a String?-type object to a String-type variable?

I would like to assign the value element of a dictionary to a String variable. In swiftUI, all value elements in dictionaries are returned as String? type. When a String? type is assigned to a String variable, the String variable displays in the…
gary
  • 85
  • 7
0
votes
1 answer

Using if let... don't understand where data is coming from

I am following the Stanford CS193 SwiftUI iOS Development Course on YouTube and I am having a very difficult time comprehending how a certain piece of the code is working. import Foundation struct MemoryGame where CardContent:…
phast
  • 188
  • 1
  • 2
  • 12
0
votes
2 answers

How can I unwrap seconds remaining to prevent it from being nil?

Why am I getting "Unexpectedly found nil while unwrapping an Optional value? I check the value of timerSeconds and it is correctly assigned to what I want it to be assigned to. However, when I call the function StartTimer my app is crashing. 300…
djumanji
  • 43
  • 5
0
votes
1 answer

Swift: How to use optional in ternary check for disabling button

I have a simple task for JS, and I have no idea how to make it using Swift. Help me please to understand and make this. I need just check if state value is or not nil, and disable the button. Button(action: {onBoardingNav.goTo(step: 3)} ) {...} …
Andrey Nadosha
  • 262
  • 1
  • 4
  • 16
0
votes
0 answers

How to unwrap an optional dictionary in swift?

I have a dictionary wrapped in an optional called optTest. After unwrapping the optional, it looks like this: ["fileName": "testName", "fileSize": 10] Given optTest, I am trying to retrieve fileName from this optional but having some trouble. I've…
smolkitty
  • 43
  • 1
  • 5
0
votes
1 answer

try? returns double optional in Swift 4

There are a lot of similar questions but no one describes how to handle exceptions with try? + double optional. I know str1 doesn't generate exception. It is just an example: let str1: String? = "some string" let str2: String? = try? str1…
Vyachaslav Gerchicov
  • 2,317
  • 3
  • 23
  • 49
0
votes
0 answers

Cast to Error fails, but cast to NSError succeds

In the userInfo of my notification, there is an error key, which contains an NSError however casting it to Error fails and NSError successes This code does NOT pass guard let error = notification.userInfo?["error"] as? Error else { …
Peter Lapisu
  • 19,915
  • 16
  • 123
  • 179