Unwrapping optionals with just `let` [duplicate] - swift

This question already has answers here:
Please Help Me Intepret This Code SWIFT
(4 answers)
What is the `?` is or the `:` at the equation when coding in Swift
(3 answers)
Closed 4 years ago.
Can someone help me understand the following code expression, how would you read it?
let myVar = (someOptional != nil) ? someOptional! : ""
If someOptional is not nil unwrap it? if this is correct, what does do the ? and the :"" in the expression?
I have always unwrapped my optionals with if lets but and I'm not sure how the above code reads.
Here is a more concrete real code example that shows how it is used...
let currentZipCode = (placemark.postalCode != nil) ? placemark.postalCode! : ""

Related

Constant used before being initialzed [duplicate]

This question already has an answer here:
Constant unassigned optional will not be nil by default
(1 answer)
Closed 4 years ago.
let errorCodestring : String?
if let theError = errorCodestring {
print(theError)
}
In the above code Xcode throws the error:
constant 'errorCodestring'being used before being initialized
What is wrong in the code and how to clear the error?
You have to initialize the constant before using it.
let errorCodestring : String? = nil

Optionals in Swift - not assigning default value for optionals [duplicate]

This question already has answers here:
Why optional constant does not automatically have a default value of nil [duplicate]
(2 answers)
Constant unassigned optional will not be nil by default
(1 answer)
Closed 5 years ago.
let company : String? // = nil
if company == nil {
var newCompany = company ?? "apple"
}
In this code identifier company we need to assign as
let company : String? = nil
but apple documentation is telling that no need for assigning nil value
You declared a constant without a value and without an initializer that can change it, which is nonsensical. The compiler, therefore, warns you that you should initialize the constant.
Change let to var and your code will work fine.

exc_bad_instruction in Swift 3.0 [duplicate]

This question already has answers here:
What does "Fatal error: Unexpectedly found nil while unwrapping an Optional value" mean?
(16 answers)
Closed 5 years ago.
I don't understand why it is coming.
Are you sure that object for key "quantity" is array of Doubles? You force unwrapping results so make sure that it is [Double]
Try this
guard let quantity: [Double] = salesByStateResponse.value(forKey: "quantity") as? [Double] else {
return
}

How do I use ?: in Swift? [duplicate]

This question already has answers here:
Does Swift have a null coalescing operator and if not, what is an example of a custom operator?
(6 answers)
Closed 6 years ago.
I love this syntax in Objective-C, where a question mark and colon let you use a backup value:
NSString const name = [self getName] ?: #"backup";
and I want to use the same in Swift, but I get this when I try:
Is there any way to do this in Swift? If not, can I write a custom infix operator to do it?
It's called a null (or nil) coalescing operator, and the Swift syntax is:
let name = getName() ?? "backup";

Swift shorthand to access property versus default value [duplicate]

This question already has answers here:
Providing a default value for an Optional in Swift?
(5 answers)
Closed 7 years ago.
Does Swift have a shorthand syntax for substituting default values when trying to access properties on an optional? For example:
let value = anOptional != nil ? anOptional.value : defaultValue
This is not a question about the simple use of the ?? coalescing operator, but a question about a shorthand syntax for assigning from a property on a non-nil optional.
You can do this let value = anOptional ?? defaultValue