This question already has an answer here:
Replace c style for-loop in Swift 2.2.1
(1 answer)
Closed 6 years ago.
I wrote the following code in swift(2.2):
for var i = 2; sqrt(Double(num)) >= Double(i); i += 1 {.....}
However, I keep getting a warning message which reads "c-style for statement is deprecated and will be removed in future version of swift".
So, what is the proper way of writing the same loop is "Swift style"? Casting the value of num as double gives me error with "Swift style". Any suggestions?
Thank you.
You can refactor your deprecated C-style for loop using the for-in construct
for i in 2...Int(sqrt(Double(num))) { }
However if you really want to go essential try defining this operator to find the square root of an int rounded down to the closest int.
prefix operator √ {}
prefix func √ (number: Int) -> Int {
return Int(sqrt(Double(number)))
}
Now you can write your for loop this way
for i in 2...(√num) { }
The For loop in Swift 2.2 is re-designed for use in iterating over a sequence, such as ranges of numbers, items in an array, or characters in a string. The condition in your For loop is not easily converted into sequence or range, so it would be best to re-write it as a While loop.
Related
Hello everyone I am new to scala and after seeing the do..while syntax which is the following:
do{
<action>
}while(condition)
I have been asked to perform this exercise which consists in predicting the output of a program containing the do..while loop.
var set = Set(1)
do {
set = set + (set.max + 1)
} while (set.sum < 32)
println(set.size)
After execution I get the following error:
end of statement expected but 'do' found
do {
I know that it is possible to convert this loop to while (which is even ideal), however I would like to know if the do..while loop still works in scala if yes, is it a syntax error (Because I searched on the net but I found the same syntax and no mention of this error)? if no, is there a version from which the loop is no longer supported?
You can still use do-while in Scala 2.13.10
https://scastie.scala-lang.org/DmytroMitin/JcGnZS3DRle3jXIUiwkb0A
In Scala 3 you can write do-while in while-do manner using Scala being expression-oriented (i.e. the last expression is what is returned from a block)
while ({
set = set + (set.max + 1)
set.sum < 32
}) {}
https://scastie.scala-lang.org/DmytroMitin/JcGnZS3DRle3jXIUiwkb0A/2
https://docs.scala-lang.org/scala3/reference/dropped-features/do-while.html
I know the difference between i++ and ++i in Swift. As the official document said, it is better to use ++i to increment i.
But I wonder why I get a syntax error using i++ in the for loop.
The code looks like this:
for var i = 0; i < 10; i++{
println("hello")
}
However, it is OK to use either i++ or ++i in other cases. Is there any restrictions in for loop?
The error says that:
Operator is not a known binary operator
The cause is very simple: you need to add a blank between the operator and the opening curly brace:
i++ {
^
without that, the compiler takes ++{ as a binary operator, with i and print("hello") as its arguments
The problem doesn't happen with the prefixed version of the increment operator because the i variable makes a clear separation between the ++ operator and the curly brace (letters and numbers cannot be used to define operators).
Trying to use Swift is just getting ridiculous. I updated Xcode from 6.2 to 6.3 and now the compiler is complaining about simple addition. The source line is this, where passKeyData is an NSData item:
let u8Value : UInt8 = 3 + passKeyData.length as UInt8 + 1
Swift is complaining about "Ambiguous use of the '+' operator".
It's a slightly misleading error message, but...
NSData.length returns an Int, and you can't convert from an Int value to a UInt8 using as. You need to explicitly construct a UInt8 like so:
let u8Value : UInt8 = 3 + UInt8(passKeyData.length) + 1
You also may want to either check the value doesn't exceed UInt8.max (or else you'll get a runtime failure), or use UInt8(truncatingBitPattern: x) if you don't mind truncating it down to fit.
I'm playing with Apple's new Swift language. Below snippet can successfully produce a result:
var a:Int64 = -7
println(a)
However, if I change the code to this:
var a:Int64 =-7
println(a)
I'll get the error:
Error:(12, 12) consecutive statements on a line must be separated by ';'
It seems that Swift thinks =- is an operator, which does not exist in Swift. If so, why generate that error?
Swift is very strict to avoid ambiguity in operators =- in var a:Int64 =-7 basically means unary prefix operator, that is undefined in this case. The =- cannot be split in two as there is no separator and =- operator can be defined any time. To avoid any ambiguity use spaces. var a:Int64 = -7 has clear separation between assignment and unary prefix operator.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Scala operator oddity
I'm very new to Scala and I read that in this language everything is an Object, cool. Also, if a method has only 1 argument, then we can omit the '.' and the parentesis '( )', that's ok.
So, if we take the following Scala example: 3 + 2, '3' and '2' are two Int Objects and '+' is a method, sounds ok. Then 3 + 2 is just the shorthand for 3.+(2), this looks very weird but I still get it.
Now let's compare the following blocks on code on the REPL:
scala> 3 + 2
res0: Int = 5
scala> 3.+(2)
res1: Double = 5.0
What's going on here? Why does the explicit syntax return a Double while the shorthand returns an Int??
3. is a Double. The lexer gets there first. Try (3).+(2).