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

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
}

Related

How can I, in a if Statement ask for multiple possible numbers, Swift [duplicate]

This question already has answers here:
How to compare one value against multiple values - Swift
(8 answers)
Closed 2 years ago.
I want to ask if the generated random Number is 1 or 13 or 25. I cut ask something like:
if randomNumber == (1) || if randomNumber == (13) || if randomNumber == (25)
and that's works but its way to much code. I try to minimize my code.
I did try something like this:
if randomNumber == (1 || 13 || 25)
but this didn't worked.
You can convert them to a simple collection like an array or something and use its methods like:
if [1, 13, 25].contains(randomNumber)
More generally:
if (<#myArray#>.contains { <#condition#> }

How can I traverse an array with stride? [duplicate]

This question already has an answer here:
How can I do a Swift for-in loop with a step?
(1 answer)
Closed 3 years ago.
I want to traverse an array with a stride. For example, I have an array [0,1,2,3,4,5,6,7,8,9].And I want to traverse this array with a stride of 3. The oc code likes below:
for (int i = 0; i < array.count; i += 3) {
}
How can I do that with swift.
You can use a for loop with stride to traverse the array
let testarray = [0,1,2,3,4,5,6,7,8,9]
for i in stride(from: 0, to: testarray.count, by: 3) {
print(testarray[i])
}

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

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

how to rewrite the following for loop in swift 3 syntax? [duplicate]

This question already has answers here:
Fix warning "C-style for Statement is deprecated" in Swift 3
(4 answers)
Decrement index in a loop after Swift C-style loops deprecated
(5 answers)
Closed 6 years ago.
for (int i = n-2; i >= 0; --i)
{
....
}
the automatic translation of the above to swift 3 syntax is this
for i in n-2 ... 0
{
}
this doesn't work, because n could be 1, in the c syntax, this is valid, the loop won't be triggered,
but in the swift 3 syntax, this will cause runtime error.
for i in stride(from: n-2, through: 0, by: -1) {
}
for i in (0 ... n-2).reversed() {
}

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.