Swift shorthand to access property versus default value [duplicate] - swift

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

Related

Swift. Check if Any is value type or reference type [duplicate]

This question already has answers here:
Check if `Any` value is object
(2 answers)
Closed 3 years ago.
Is it possible to check if an object (not as in OOP) is of reference type or value type?
let something : Any = getSomething() // func getSomething() -> Any
let isReferenceType : Bool = // how to check?
EDIT:
As pointed out this is practically a duplicate of Check if `Any` value is object
This is not so easy as it seems, see
let isReferenceType: Bool = type(of: something) is AnyClass
See How to test whether generic variable is of type AnyObject
However, if you need such things, then usually you have some big problem with your architecture that you should address instead. Using Any type should be the last resort for exceptional situations.

What is the ?? operator? [duplicate]

This question already has answers here:
?? operator in Swift
(5 answers)
Closed 4 years ago.
What is the ?? operator
The Swift Programming Language (Swift 4.1)
I was reading the book The Swift Programming Language (Swift 4.1) by apple and I got to a part where it talks about the ?? operator:
Expert
Another way to handle optional values is to provide a default value using the ?? operator. If the optional value is missing, the default value is used instead.
Excerpt From: Apple Inc. “The Swift Programming Language (Swift 4.1).” iBooks. https://itunes.apple.com/us/book/the-swift-programming-language-swift-4-1/id881256329?mt=11
Problem
Optional Values
In the expert it says that the ?? operator is for a optional value I was wondering what it does for a Optional Value.
What it does
So the other thing I wanted to know is what does the ?? operator do.
Nil-Coalescing Operator (??)
The nil-coalescing operator (a ?? b) unwraps an optional a if it contains a value, or returns a default value b if a is nil. The expression a is always of an optional type. The expression b must match the type that is stored inside a.
?? - indicates default value assignment, when your variable has a nil value.
Consider following example:
var int a = nil // var a with nil value assignment
var int b = a ?? 1 // first trying to get value from a. If var a has nil then it will try assign value 1.
print("b = \(b)")
result: b = 1

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.

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";

Why can I use a constant initialization as if-condition? [duplicate]

This question already has answers here:
How is Swift `if let` evaluated?
(5 answers)
Closed 7 years ago.
While reading the official Apple Guide I found this
var optionalName: String? = "John Appleseed"
var greeting = "Hello!"
if let name = optionalName {
greeting = "Hello \(name)"
}
There is a constant declaration and assignment where I — as a beginner — expected an expression returning a boolean. But the condition of this if statement seems to get true as the value, because the code inside the parentheses is being executed.
Does the initialization of a constant return a boolean value or something that a if statement can use as a condition?
This is called Optional Binding. Quoting from the Basics -> Optionals -> Optional Binding section of the Swift Language Guide.
You use optional binding to find out whether an optional contains a
value, and if so, to make that value available as a temporary constant
or variable. Optional binding can be used with if and while statements
to check for a value inside an optional, and to extract that value
into a constant or variable, as part of a single action.
What this if let construct is doing is checking if someOptional is exists. If it isn't nil/.None then it "binds" the Optional value to a constant.
if let constantName = someOptional {
...
}
This can be thought of as:
if someOptional != nil {
let constantName = someOptional!
....
}