无法为类型&#39; Double&#39;调用初始化程序。使用类型&#39;(范围<double>)&#39;

时间:2017-12-04 12:06:40

标签: ios swift swift4

我在这里收到此错误(无法为类型&#39; Double&#39;调用类型&#39;(范围)&#39;)的参数列表调用初始化程序

for dista in Double(0..<(car.distance)) //error here {

        }

其中&#34;距离&#34;是Double类型。 我怎么解决呢?

更新

var距离:双倍?是一个定制的班级&#34; Car&#34;。我有一个功能,为我下载不同的汽车

    if let carsDownloaded = response.cars {
                    var number = cars / (models?.count)!
                    number = number == 0 ? 1 : number

                    let distanceC = carsDownloaded

         for car in distanceC {
                        for dista in car.distance! { 
   }
 } 

汽车是我可以下载的汽车,如果我真的可以下载这些汽车的响应选择,最后response.cars是我将下载的汽车。现在我想在过滤器中订购那些离最近最公平的汽车,但我的问题是我从我的位置到具有外部功能的X车的距离值

func distance(from: CLLocationCoordinate2D, to: CLLocationCoordinate2D) -> CLLocationDistance {
        let from = CLLocation(latitude: from.latitude, longitude: from.longitude)
        let to = CLLocation(latitude: to.latitude, longitude: to.longitude)

        return from.distance(from: to)

    }


    func didSelectt(car:Car) {

        guard let coordinates = car.location  else {
            return
        }
        self.destination = coordinates

        // update distance
        if currentLocation != nil {
            let dist = distance(from: currentLocation!, to: coordinates)
        }
    }        

1 个答案:

答案 0 :(得分:1)

循环中in参数的类型必须是Int的范围。此外,您无法将范围转换为类型,例如Double

迭代序列!= Int您需要stride

let distance = 25.0

for dista in stride(from:0.0, to: distance, by: 1.0) {
    print(dista)
}