Optional binding is a datatype, often used in Swift, particularly Swift3 onwards.
Questions tagged [optional-binding]
44 questions
153
votes
8 answers
Conditional Binding: if let error – Initializer for conditional binding must have Optional type
I am trying to delete a row from my Data Source and the following line of code:
if let tv = tableView {
causes the following error:
Initializer for conditional binding must have Optional type, not
UITableView
Here is the full code:
// Override…

Del Hinds
- 2,887
- 4
- 12
- 12
5
votes
1 answer
How to use optional binding in Swift 2
I'm new to learning Swift so I decided I might as well learn Swift 2 instead. Everything has made sense to me so far except for the following code snippet. Hopefully someone can shed some light on this for me.
//: Playground - noun: a place where…

jobber80
- 53
- 1
- 3
4
votes
5 answers
How to use optional binding in switch statement in prepare(segue:)
In swift you can use a cool feature of the switch statement in prepare(segue:) to create cases based on the type of the destination view controller:
Example:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
switch…

Duncan C
- 128,072
- 22
- 173
- 272
3
votes
4 answers
Using optional binding when SwiftUI says no
The problem
TL;DR: A String I'm trying to bind to inside TextField is nested in an Optional type, therefore I cannot do that in a straightforward manner. I've tried various fixes listed below.
I'm a simple man and my use case is rather simple - I…

Jordan Niedzielski
- 93
- 7
3
votes
1 answer
Inconsistent Casting of Any? into a Protocol in Swift during Optional Binding
I'm having trouble with some Swift Optional Binding with a cast into a protocol. I have the following code in a playground that works fine.
protocol CodeCollection {
var name: String { get }
var codes: [String] { get }
}
struct…

vichudson1
- 881
- 1
- 10
- 21
3
votes
3 answers
Is there a technical downside to using implicitly-unwrapped optionals but testing for nil vs optional binding?
Okay, so I know the normal way to use optionals in Swift is via optional bindings to unwrap them, like so...
let stringA:String? = nil // (or "ABC")
if let unwrappedStringA = stringA
{
let x:String = unwrappedStringA
}
But I've also seen the…

Mark A. Donohoe
- 28,442
- 25
- 137
- 286
3
votes
1 answer
Swift style: Function returns optional of type that you need in order to continue, what's the best practice to handle this?
First, I'm going to use cellForRowAtIndexPath as my example since the de-queue function returns an optional and ignore the fact that it's perfectly safe to explicitly unwrap this.
My question is: what is considered the "best" way or style to handle…

lanza
- 1,512
- 2
- 13
- 26
3
votes
2 answers
Only calling a function when an optional is not nil in one expression?
I know it is possible to do this like so:
let intValue: Int? = rawValue == nil ? Int(rawValue) : nil
Or even like this:
var intValue: Int?
if let unwrappedRawValue = rawValue {
intValue = Int(unwrappedRawValue)
}
However I'm looking to find…

Jack Wilsdon
- 6,706
- 11
- 44
- 87
3
votes
1 answer
Does the underscore symbol ignore or check for nullity in a switch statement in Swift?
I have a switch statement in Swift like this:
switch tuple {
case (let someObject, let current, nil):
return true
// Other cases...
}
The tuple is of type (SomeObject?, SomeObject, SomeObject?), and what I'm saying in English is:…

Matthew Quiros
- 13,385
- 12
- 87
- 132
2
votes
2 answers
Unwanted behaviour with optional binding evaluating optional(nil)
After updating xCode to version 10 (and swift 4.2), I have a strange behaviour on optional bindings
The code is below, and it ´s about reading json file, T is a generic type (here String)
// Are there values for configName ?
if let values =…

eqtèöck
- 971
- 1
- 13
- 27
2
votes
1 answer
Swift Pattern Matching - Switch, downcasting, and optional binding in a single statement
Is there a way to consolidate the switch statement below to include the optional binding?
A little background context...
First here's my result type:
enum Result {
case success(Value)
case failure(Swift.Error)
}
An extension on…

edelaney05
- 6,822
- 6
- 41
- 65
2
votes
2 answers
Swift Initializer for conditional binding must have Optional type, not '[AVCaptureDevice]'
I have this following code in my swift project
if let availabeDevices = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInMicrophone,
.builtInWideAngleCamera], mediaType: AVMediaType.video, position: .back).devices {
captureDevice…

askoasksao
- 87
- 1
- 7
2
votes
1 answer
Is it possible to use the variable from an optional binding within the same conditional statement?
if let popupButton = result?.control as? NSPopUpButto {
if popupButton.numberOfItems <= 1 {
// blahblah
}
}
I want to avoid the double nested if.
if let popupButton = result?.control as? NSPopUpButton && popupButton.numberOfItems <=…

A O
- 5,516
- 3
- 33
- 68
2
votes
2 answers
OR condition for optional binding?
I've seen in Swift 3.1 documentation that you can include several optional bindings in an if statement separated by commas, and that it behaves like an AND operator.
Let's say that I have two optional properties and I'd like to check which of them…

AppsDev
- 12,319
- 23
- 93
- 186
2
votes
1 answer
Optional binding bug on Swift 2.2?
if let mathematicalSymbol = sender.currentTitle {
brain.performOperation(mathematicalSymbol)
}
The code above introduces the error below;
Value of optional type 'String?' not unwrapped; did you mean to use
'!' or '?'?
As can be seen in this…

emreerokyar
- 359
- 3
- 12