Why is x? = y valid syntax in Swift? [duplicate] - swift

This question already has answers here:
Swift optionals - Why does var a:Int? a? = 4 return nil
(2 answers)
Closed 3 years ago.
I am surprised that this compiles
var x: Int? = 3
x? = 5
This seems to do the same thing as x = 5, but it doesn't make sense to me that this would be allowed at all. Would it ever behave differently (like if x was a different type, or if it were a property)?

When you make x optional
x? = 5
if x originally is nil then the line won't run , otherwise it'll act as x = 5

Related

Fahrenheit to Celsius convert and printing problem [duplicate]

This question already has answers here:
Concatenate number with string in Swift
(7 answers)
Closed 3 years ago.
Hi i was trying to create a function that printed out the statement f(variable)degrees Fahrenheit = c(variable) degrees Celsius in swift and I am having issues printing it mainly cause its a variable with numbers and im trying to put it into a string I tried some different types of decleratino but it hasnt worked. and sorry for repetitive questions
var f = 68.0;
var c = (f - 32.0) * 5.0 / 9.0;
var conversion = "f degrees Fahrenheit = c degrees Celsius"
print(conversion)
You need to use String interpolation, otherwise it's just text.
Replace c with \(c) to actually get the value of variable c value.
Also f with \(f) for the same reason.
var conversion = "\(f) degrees Fahrenheit = \(c) degrees Celsius"

Swift 3: Remove one character from set [duplicate]

This question already has answers here:
Removing duplicate elements from an array in Swift
(49 answers)
Closed 5 years ago.
Say I have a set [A, B, B, C, D], how would I remove just the first B?
If charToDelete = B and I do this:
SlidingWin.remove(charToDelete)
won't it remove all of the B characters?
In Swift lingo, what you have is an array (not a set). If you want to remove the first "B" from an array, you can do this:
if let index = array.index(of:"B")
{ array.remove(at:index) }
[EDIT] example of a functional approach for anagrams:
let set1 = "cabb"
let set2 = "cbabeijbbacbkiie"
let anagrams = zip(set2.indices,set2.indices.dropFirst(set1.count-1))
.map{set2[$0...$1]}
.filter{$0.sorted() == set1.sorted()}

Rewrite C-style for loop in Swift 3 [duplicate]

This question already has answers here:
For-In Loops multiple conditions
(5 answers)
Closed 5 years ago.
I have a C-style for loop that's no longer supported in Swift 3. It looks something like this:
for (var x = 0; x < foo.length && x < bar.length; x++) {}
What's the equivalent of this that's available now in Swift 3?
if you only need to check the two limits, how about this
for x in 0..<min(foo.length, bar.length)
{
// do stuff
}

How to quickly check if an Integer lies within a given range [duplicate]

This question already has answers here:
Can I use the range operator with if statement in Swift?
(6 answers)
Closed 6 years ago.
I have an integer x and I want to check if it lies between a given boundary / within a given range.
The straightforward approach would be
let contains = x > lowerBounds && x < higherBounds
Is there a more swifty approach to this?
You can create a range and check if it contains x:
let contains = (lowerBounds...upperBounds).contains(x)
e.g.:
let successful = (200..<300).contains(httpStatusCode)
Or you can use the pattern matching operator:
let contains = lowerBounds...uppperBounds ~= x

Is there an easier way to get the lesser of two values in Swift [duplicate]

This question already has answers here:
Swift equivalent for MIN and MAX macros
(5 answers)
Closed 8 years ago.
I'd like to assign the lesser of two values to a variable. In Ruby I would do something like:
my_var = [value_one, value_two].min
In Swift, of course, I can do this:
var myVar = 0.0
if valueOne < valueTwo {
myVar = valueOne
} else {
myVar = valueTwo
}
But, I'm wondering if there is a cleaner, more succinct solution.
var myVar = min(valueOne, valueTwo)
min is a standard library function that takes the lesser of two (or least of several — it's variadic) Comparable values.