Swift (SwiftUI) programming syntax operator meaning? [closed] - swift

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 months ago.
Improve this question
I don't know how to word the question to find help in a search engine. Basically I'm trying to figure out the meaning of this operation
self.isFromCurrentUser = fromId == Auth.auth().currentUser?.uid
The variables are NOT the question because I already know about Firebase and whatnot. Rather the question is why is there an assignment operator = and an == in this line of code?
variable1 = variable2 == variable3
What does this mean? What is variable1 getting assigned to?

The statement
variable1 = variable2 == variable
assigns variable1 the value of the boolean (true or false) of the result of variable2 == variable3 (if they are equal).
It's equivalent to
if variable2 == variable3 {
variable1 = true
} else {
variable1 = false
}

Related

How to Check if Variable exists in Scala [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
I want to check if a variable is already defined/exists in scala or not. Lets say a function called checkVar do this operation:
var x = 10
checkVar(x) -> returns boolean True
checkVar(y) -> returns boolean False
I am asking this question because I want to create a mechanism to define a variable if it doesn't exist.
Variables only exist at compile time so you can't dynamically create or delete variables at runtime. So both x and y must be defined at compile time or else the compiler will reject the code.
What you can do is use Option to indicate whether a variable has a value or not:
def checkVar(v: Option[Int]) = v.nonEmpty
var x = Some(10)
checkVar(x) // True
val y = None
checkVar(y) // False
x = None
checkVar(x) // False

Print repetition in Swift while statement [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
In my while statement, I cannot understand why my output is printed twice ?
I would like to print i only one time, where is my error ?
func fetch2(){
var i: Int = 0
while i <= (self.returned-1) {
let itemLookUp = "https://shopping.yahooapis.jp/ShoppingWebService/V1/json/itemLookup?appid=\(self.appId)&itemcode=\(self.arrayCodeProduct[i])&responsegroup=large"
print(i)
i = i+1
}
}
Here is the output that I obtain :
0
1
2
3
0
1
2
3
Thank you in advance.
It looks like fetch2() is called twice.
Add a print(#function) before you var i and check that fetch2() is not called several times.

How to check if the result of an operation equals an Integer? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm super new with Swift (and programming). I need to compare if the result of a Remainder Operator (modulo operator) equals an integer.
This is my code:
if Int.random(in: 1...1000) % 4 >= "This number" {
print ("OK")
} else {
print ("WRONG")
}
If "This number" is replaced by an actual number e.g. 100 it works fine.
But if replaced by Int it crashes displaying: Type 'Int.Type' cannot conform to 'BinaryInteger'; only struct/enum/class types can conform to protocols
You have to compare Int with Int, not String or Int type (Int.Type). If you want to declare a value that you will use for comparison you can declare it as variable. To compare if it is equal use == operator.
let thisNumber: Int = 3 // or any other value
if Int.random(in: 1...1000) % 4 == thisNumber {
print ("OK")
} else {
print ("WRONG")
}
now both Int.random(in: 1...1000) % 4 and thisNumber is Int so you can compare them without getting an error.

Display only some digits of an Int [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Let's say you have an Int.
let int = 12345
I want it to only display some of the digits.
For example: print(firstTwoDigits) --> 12
How do I do this and thank you in advance.
It depends on your specific requirements.
This prints the first two digits of an integer number
let intVal = 12345
print(String(intVal).prefix(2))
Output: 12
Another way which only prints certain ones in the number:
let intVal = 12345
let acceptableValues = ["1", "2"]
let result = String(intVal).filter {
acceptableValues.contains(String($0))
}
print(result)
Output: 12

Modify variable in for loop [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
The code below would print abc 5 times and then print 1024. As far as I understand, in any for, the "iterator " is automatically declared (the equivalent of a C(++)/Java for(int i=1; i<=5; i++) ). Is it possible to actually not automatically create that variable and use the one declared before the for so that it would print abc 5 times and then print 5, thus modifying it?
var i = 1024
for i in 1...5 {
print("abc")
}
print(i)
#DrummerB's answer works, but if you want a for...in loop, this will also work. It's the same principle - declare your variable outside the loop and increment it inside it:
var i:Int = 0
for _ in 0...5 {
print("abc")
i += 1
}
print(i)
Since you aren't referencing a loop variable, Swift syntax recommends an underscore.
You could just rewrite the for loop as a while loop like this:
var i = 1024
i = 1
while i <= 5 {
print("abc")
i = i+1
}
print(i)
If you really want to change the value of the i using the loop in that way, you can do:
var i=1024
for j in 1...5 {
print("abc")
i = j
}
print(i)
The j would be used strictly in the loop, so once finished, it's value is dumped. But a variable declared before (i in your case), could take it's value and maintain it.