Optional binding is a datatype, often used in Swift, particularly Swift3 onwards.
Questions tagged [optional-binding]
44 questions
2
votes
1 answer
Why use optional binding?
I am aware that this question is here, but it only partially answers my question and I cannot comment on the answer so I'm forced to post here.
What is the difference between optional binding and just simply using ?. My understanding is when you use…

Josh
- 529
- 6
- 21
1
vote
1 answer
Swift3, unwrapping Optional (in SQLite.swift)
In the most excellent SQLite.swift, I have
let stmt = try local.db!.prepare(..)
for row in stmt {
for tricky in row {
each "tricky" is an Optional
The only way I know to unwrap each tricky is like this
var printable:String = "
if let…

Fattie
- 27,874
- 70
- 431
- 719
1
vote
2 answers
Swift Optional binding, why the need for a local var?
I'm reading Apple's developer documentation on Optional Binding
Why can't I use:
if someOptional? {
statements
}
Instead of
if let constantName = someOptional {
statements
}
Why when there is no need for a local variable or constant?

Nutela
- 85
- 7
1
vote
1 answer
Naming convention for optional binding
One thing that originally discouraged me from incorporating too much optional binding in my code was the addition of more variable names. For example, I'd generally write:
if bananasInBarrel != nil{
print("We have \(bananasInBarrel!) bananas in…

Rogare
- 3,234
- 3
- 27
- 50
1
vote
4 answers
How do you perform optional binding on an optional of optional?
How do you do optional binding on an optional of optional?
For example, assume UIViewController's navigationController property is an optional of optional. Should I use method #1 or method #2, or is there a third way?
Method #1
if let…

Boon
- 40,656
- 60
- 209
- 315
0
votes
2 answers
OptionalBinding using Guice to avoid binding by users
Following the documentation of OptionalBinder
An API to bind optional values, optionally with a default value.
OptionalBinder fulfills two roles:
It allows a framework to define an injection point that may or may not be bound by users.
It allows…

Naman
- 27,789
- 26
- 218
- 353
0
votes
0 answers
Property with value nil is having "unreadable data" and causing crash. Swift 4.2 | iOS
I have declared an optional property of type String. In viewDidload, I call a function which performs optional binding on this property:
public var localMsgIdToBeHighlighted: String? = nil
Following is my method:
private func…

Peeyush karnwal
- 622
- 7
- 24
0
votes
2 answers
In Swift How do I iterate over an array getting 2 variables when each pair of elements is a String? and a String
I want to iterate over an array String?, String repeated pair but I cannot form the "for case let (a,b) in array" correctly.
The best I have come up with is to create a temp struct of {String?, String} and create an array of the temp structs and…

Richard Legault
- 443
- 4
- 9
0
votes
3 answers
Why value of type 'UIView' has no member from reference UIView variable?
I want to reference a variable to a specific function.
However, there is an error called Value of type 'UIView' has no member 'lineTo'
Clearly, the whatSelectObject variable contains the classes in which a member exists.
So I used If statement,…

bakuiseok
- 41
- 1
- 10
0
votes
1 answer
Initialize and bind swift optional member at the same time?
My class contains optional member properties like this:
class PauseRenderTarget: RenderTarget {
var background: SKShapeNode? = nil
var resume: Entity?
var restart: Entity?
var reset: Entity?
func createEntities()
}
When I…

chustar
- 12,225
- 24
- 81
- 119
0
votes
0 answers
Issue with optional binding using lazy var
I am working with a UIPageViewController and I have an array of viewcontrollers
lazy var subviewControllers: [UIViewController] = {
return[
UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier:…

Fabio
- 1,913
- 5
- 29
- 53
0
votes
2 answers
What is the process to translate optional binding into error handling in Swift?
I'm working through an OAuth tutorial that uses the Locksmith framework to store tokens. The tutorial is written in an older version of Swift and also with an older version of the Locksmith framework. While recreating the example in Swift 4, I…

Laurence Wingo
- 3,912
- 7
- 33
- 61
0
votes
1 answer
Check Non Optional value for nil
I'm creating a function in swift to check the if non optional value return nil. My aim is just to handle that exception and avoid app crash for unexpected nil values.
I have two variables in my class:
// My Class variables
var compulsoryValue:…

developer
- 668
- 1
- 6
- 24
0
votes
2 answers
Swift optional binding constant names
I am just transitioning from Objective-C to Swift and have been constantly writing the following code for optional binding,
if let tempX = X {
}
My question is I have to do it so often that I need to find a new name for constant every time. What's…

Deepak Sharma
- 5,577
- 7
- 55
- 131
0
votes
0 answers
multiple optional binding for a immutable variable and an immutable
I want to have a multiple optional binding for a mutable and an immutable optional strings. but this code has an error
var name: String?
let family: String?
if var temp1 = name, temp2 = family{
print("ok")
}
error: constant family used before…

آژانس کتاب
- 57
- 5