Check if indexPath.row is row 1-10? [duplicate] - swift

This question already has answers here:
Can I use the range operator with if statement in Swift?
(6 answers)
Closed 5 years ago.
if indexPath.row == 0 ... 11 {
}
That isn't working, it says Binary operator '==' cannot be applied to the operands of type 'Int' and 'CountableClosedRange.
(Purpose of this is to disable rows 0-12.)
What is the proper way to do this? Basic question but I don't know what to search Google for. Thanks in advance!
If I had to guess it would be:
for numero in 0 ... 11 {
if indexPath.row == numero {
}
}

For check between value
Use "~=" range operator.
And use as below
Ex.
if 0 ... 11 ~= indexPath.row {
print("IndexPath in between 0 to 11")
}

let indexPath = IndexPath(row: 4, section: 0)
if (0...10).contains(indexPath.row) {
print(indexPath.row) // 4
}
This checks whether the row of your indexPath is within the range of 0 - 10

You are comparing a range of integers against a single integer which does not work.
You need to check if the range contains the value
if (0...11).contains(indexPath.row) { ...
But in your case you can also simply check
if indexPath.row < 12
since the row will never be negative.

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#> }

Swift loop from upper value to lower value [duplicate]

This question already has answers here:
How to iterate for loop in reverse order in swift?
(16 answers)
Closed 4 years ago.
I have a Swift for loop defined like this:
for i in 20...1 {
array.append(i)
}
But I get a crash with message
Thread 1: Fatal error: Can't form Range with upperBound < lowerBound
What is the fix?
You need to reverse the range:
for i in (1...20).reversed() {
array.append(i)
}
You cannot loop in reverse order like that, if you want you can try this :
for i in stride(from: 20, through: 1, by: -1) {
array.append(i)
}

Swift 4: modifying numbers in String [duplicate]

This question already has answers here:
Leading zeros for Int in Swift
(12 answers)
Closed 5 years ago.
In my application i create multiple files and i would like them to have consecutive numbering as part of the name. For example log_0001, log_0002
I use string interpolation, so in code it looks like
fileName = "log_\(number)"
number = number + 1
However, if i keep number as just Int, i will have log_1 instead of log_0001
I've only figured that i could check if number has 1/2/3/4 digits and add '000/00/0' manually.
Are there any String modifications allowing to put the required number of '0' to make it 4 symbols?
"I've only figured that i could check if number has 1/2/3/4 digits and add '000/00/0' manually."
In this case try the following code: Save the digit length in a variable, for example digitLength.
if (digitLength == 1) { print("log_000")
} else if (digitLength == 2) { print("log_00")
} else if (digitLength == 3) { print("log_0")
}
else { print("log_") }
print(number)
and then the variable.

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() {
}

How to properly use conditionak operator in Swift [duplicate]

This question already has answers here:
Swift ternary operator compilation error
(2 answers)
Closed 6 years ago.
I want to rewrite the below code to use the conditional operator:
if indexPath.row == 0 {
cell.indentationLevel = kIndentFDA
} else {
cell.indentationLevel = kIndentCompleted
}
But when I write this:
indexPath.row == 0 ? cell.indentationLevel = kIndentFDA : cell.indentationLevel = kIndentCompleted
I get this compiler warning:
"Cannot assign to immutable expression of type 'Int'"
The problem is that you don't understand what the ternary operator is. It isn't for performing assignments within the branches; the branches are not executable statements. Rather, the branches are evaluated expressions; thus, you use the ternary operator in the rvalue of an assignment and the branch values themselves are what is assigned.
So, you are writing this (which is nonsense):
indexPath.row == 0 ? cell.indentationLevel = kIndentFDA : cell.indentationLevel = kIndentCompleted
What you mean is:
cell.indentationLevel = indexPath.row == 0 ? kIndentFDA : kIndentCompleted
I don't know what the warning it but it probably can't figure out the order to apply the operators. This should work.
cell.indentationLevel = indexPath.row == 0 ? kIndentFDA : kIndentCompleted