Optional binding is a datatype, often used in Swift, particularly Swift3 onwards.
Questions tagged [optional-binding]
44 questions
0
votes
0 answers
The returns is not conform to the method return type but it still works
The following code snippet is simplified from the Optional Chaining chapter of the official document.
In short, the class Person has an optional stored property address of type Address. By offer some/all of the property buildingName, buildingNumber…

SLN
- 4,772
- 2
- 38
- 79
0
votes
2 answers
Making a variable from if statement global
While encoding JSON, I´m unwrapping stuff with an if let statement, but I'd like to make a variable globally available
do {
if
let json = try JSONSerialization.jsonObject(with: data) as? [String: String],
let jsonIsExistant =…

Nils
- 1,755
- 3
- 13
- 25
0
votes
5 answers
Guarding against a switch button having a nil optional value
I have a piece of code that runs if a switch has been set in settings as follows:
UserDefaults.standard.bool(forKey: "signatureSwitchState")
let buttonState = UserDefaults.standard.object(forKey: "signatureSwitchState") as! Bool
if…

Tom
- 790
- 2
- 9
- 32
0
votes
1 answer
Fail-safe injector for child modules. Optional bindings in google guice
I have a child module called ChildPlugin and I inject classes from main module as follows:
public class ChildPlugin {
private ExampleClass demo;
@Inject
public void setDemo(ExampleClass demo) {
this.demo = demo;
}
}
The…

burak emre
- 1,501
- 4
- 22
- 46
0
votes
3 answers
Optional Binding, what exactly the word "Binding" means here?
Optional binding is a method to find out whether an optional contains a value, and if so, to make that value available as a temporary constant or variable.
var possibleNumber: Int? = 123
if let actualNumber = Int(possibleNumber){
…

SLN
- 4,772
- 2
- 38
- 79
0
votes
1 answer
swift: safely access dictionary of SCNNode arrays
I'm relatively new to Swift and have yet to master the safety aspects of optionals.
I have a dictionary of type [String: [SCNNode]]. A given molecule will have multiple components as elements in an [SCNNode]. For that molecule I retrieve this array…

bpedit
- 1,176
- 8
- 22
0
votes
1 answer
Optional Character is not getting removed even after optional binding
I am setting a optional property from previous view controller while pushing but even after optional binding its printing optional character in console. Here is my code
if let otp = self.confirmationCode {
print(otp)
}
It give output as…

Vikas
- 914
- 7
- 19
0
votes
2 answers
Performing a method that uses variables that come from optional
If I need to perform a method whose multiple parameters' original source are optional, is doing multiple optional binding before performing the method the cleanest way to go about this?
e.g. UIStoryboardSegue's sourceViewController and…

Boon
- 40,656
- 60
- 209
- 315
-1
votes
1 answer
Why doesn't this optional binding work as expected?
I'm in the process of introducing Swift to an Objective-C app.
I have a UIViewController subclass where I want to hide a button if a certain property is nil. My Swift code looks like this, using optional binding:
if let optionalVar =…

bpapa
- 21,409
- 25
- 99
- 147
-1
votes
2 answers
unable to use optional int "possibleNumber" in optional binding
I'm new to Swift and is trying to learn the concept of optional binding. I have came up with the following code:
let possibleNumber = Int("123")
possibleNumber.dynamicType
if let actualNumber = Int(possibleNumber){
print("\(possibleNumber) has…

Thor
- 9,638
- 15
- 62
- 137
-2
votes
2 answers
Swift Optional binding behaviour unclear
I'm brand new to Swift and unable to wrap my head around a code snippet that uses Optional binding. Here is the code:
struct AWorkingView: View {
var frameSize: CGSize?
init(frameSize: CGSize? = nil) {
self.frameSize =…

borarak
- 1,130
- 1
- 13
- 24
-2
votes
3 answers
How to check multiple values in negative expression with optional binding?
I want to check if two optional variables are all null. For example, in C,
int n1 = 1;
int n2 = 2;
if( n1!=0 && n2!=0 ) {
// do something.
}
Is there any way to do this in Swift?

GT Kim
- 329
- 1
- 4
- 16
-2
votes
2 answers
Swift 3 better way to unwrap an optional
Right now I have some code that looks like the following:
let msg: String? = myStr.removingPercentEncoding ?? nil
print("msg \(msg!)")
I really don't like the use of ! in msg! as that can possibly throw an exception, I believe.
What's the best way…

randombits
- 47,058
- 76
- 251
- 433
-2
votes
3 answers
Print a nil object compiler said "unresolved identifier" What happened behind?
I've tried the following code and I got an error.
var possibleNumber = "Hello World!"
if let actualNumber: Int = Int(possibleNumber) {
print("\"\(possibleNumber)\" has an integer value of \(actualNumber)")
} else {
…

SLN
- 4,772
- 2
- 38
- 79