Use of unresolved operator <=

2019-03-06 17:10发布

I am trying to use Swift 3 for-loops, but I have been unsuccessful. Here is what I have:

for assumedPayRate: Double in (0.25..<=billRate) where assumedPayRate += 0.25 {

On the ..<= it gives me the error:

Use of unresolved operator '..<='

Here is the original Swift 2 version:

for var assumedPayRate:Double = 0.25; assumedPayRate <= billRate; assumedPayRate += 0.25 {

How can I fix this error?

4条回答
ら.Afraid
2楼-- · 2019-03-06 17:49

You can use an advanced operator to do the job, but you need to write it your own. The official documentation may help.

查看更多
三岁会撩人
3楼-- · 2019-03-06 18:03

Swift has two range operators - ... and ..<.

The ... operator corresponds to

for i = start ; i <= end ; i++

while ..< corresponds to

for i = start ; i < end ; i++

in pseudocode.

You can use ... operator for ¼ step (i.e. 0.25):

for r in 1 ..< 4*billRate {
    let assumedPayRate = r / 4.0
    ...
}
查看更多
SAY GOODBYE
4楼-- · 2019-03-06 18:04

There is no ..<=, you need to convert your statement to while

var assumedPayRate = 0.25
while assumedPayRate <= billRate {
   ... // processing

   assumedPayRate += 0.25
}
查看更多
做自己的国王
5楼-- · 2019-03-06 18:07

The operator ..<= doesn't exist in Swift and your syntax is invalid. Use stride instead:

// Assuming billRate is a Double
for assumedPayRate in stride(from: 0.25, through: billRate, by: 0.25) {
    // Do your things
}
查看更多
登录 后发表回答