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

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

Related

Swift: remove all whitespace characters from a string [duplicate]

This question already has answers here:
How to remove whitespaces in strings in Swift?
(11 answers)
Closed 4 months ago.
I'd like to use CharacterSet.whitespacesAndNewlines to remove whitespace from a string.
However, the code below does not compile.
myString.replacingOccurrences(of: CharacterSet.whitespacesAndNewlines, with: "")
// Instance method 'replacingOccurrences(of:with:options:range:)' requires that 'CharacterSet' conform to 'StringProtocol'
What is a simple way to achieve the intended result?
You can use
let result = myString.filter { !$0.isWhitespace }

How to fix 'characters' is unavailable: Please use String directly in swift 5 [duplicate]

This question already has answers here:
How to filter characters from a string in Swift 4
(2 answers)
Closed 3 years ago.
i've some code to filtering number inside variable.
Here's the code:
var numbers = String(anotherNumbers.characters.filter { "01234567890.".characters.contains($0) })
In the swift 3, this code working correctly. But in the Swift 5, i get an error 'characters' is unavailable: Please use String directly
How to fix this error?
Thankyou.
You can remove characters to use String directly.
For example
var anotherNumbers = "0123456789"
var numbers = String(anotherNumbers.filter { "01234567890.".contains($0) })
returns "0123456789"

Unwrapping optionals with just `let` [duplicate]

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! : ""

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

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