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

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.

Related

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

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
}

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.

Passing by reference and assigning into a variable [closed]

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 3 years ago.
Improve this question
I was studying about passing by reference. It made me wonder what would happen in the following example (Written in pseudo-C which supports "by reference"):
int foo(int a) {
a = 5;
return a * 2;
}
int main() {
int a = 1;
a = foo(a);
printf("%d",a);
return 0;
}
What should be printed? if we only did foo(a); without assigning into a then we would get 5. but what would be printed when assigning? should it be 5 or 10?
Since you have a = foo(a); in your main() function, a will contain the result returned by foo(a). foo(a) will always return 10 no matter what a is.
C does not support pass by reference. Changing a = foo(a); to just foo(a); would mean a would retain the value it had before it was passed to foo(), so it would be 1.
One variation of C that supports pass by reference is C++. In C++, you could write foo() as:
int foo(int &a) {
a = 5;
return a * 2;
}
The int &a syntax is used to denote that the parameter will be passed by reference. Now, foo will assign 5 to the referenced variable, but still always return 10.
In this case a = foo(a); will result in a having the value 10, and foo(a); alone will result in a having the value 5.

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.