1

我正在尝试构建我的第一个 Swift 应用程序。在此应用程序中,我正在循环一个包含有关某些餐厅的信息的 KML 文件,并且对于每个餐厅,我尝试Place使用可用信息构建一个对象,比较距离并保持Place最接近给定点的距离.

这是我的 Place 模型,一个非常简单的模型(Place.swift):

import Foundation
import MapKit

class Place {

    var name:String
    var description:String? = nil
    var location:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude:0, longitude:0)

    init(name: String, description: String?, latitude: Double, longitude: Double)
    {
        self.name = name
        self.description = description
        self.location = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
    }

    func getDistance(point: CLLocationCoordinate2D) -> Float
    {
        return Geo.distance(point, coordTo: self.location)
    }
}

这是应用程序中循环 KML 文件中项目的部分。

NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {(response, data, error) in
    let xml = SWXMLHash.parse(data);

    var minDistance:Float = Float(UInt64.max)
    var closestPlace:Place? = nil
    var place:Place? = nil

    for placemark in xml["kml"]["Document"]["Folder"]["Placemark"] {

        var coord = placemark["Point"]["coordinates"].element?.text?.componentsSeparatedByString(",")

        // Create a place object if the place has a name
        if let placeName = placemark["name"].element?.text {

            NSLog("Place name defined, object created")

            // Overwrite the place variable with a new object                
            place = Place(name: placeName, description: placemark["description"].element?.text, latitude: (coord![1] as NSString).doubleValue, longitude: (coord![0] as NSString).doubleValue)

            var distance = place!.getDistance(self.middlePosition)

            if distance < minDistance {
                minDistance = distance
                closestPlace = place
            } else {
                NSLog("Place name could not be found, skipped")
            }
        }
    }

当计算距离时,我在这个脚本中添加了断点。变量的值为placenil,我不明白为什么。如果我替换这一行:

place = Place(name: placeName, description: placemark["description"].element?.text, latitude: (coord![1] as NSString).doubleValue, longitude: (coord![0] as NSString).doubleValue)

通过这一行:

let place = Place(name: placeName, description: placemark["description"].element?.text, latitude: (coord![1] as NSString).doubleValue, longitude: (coord![0] as NSString).doubleValue)

我可以看到我的 place 对象现在已正确实例化,但我不明白为什么。当我尝试保存最近的地方时,我也遇到了完全相同的问题:

closestPlace = place

在检查器中,closestPlace即使在使用我的位置对象设置后,值也是 nil。

4

1 回答 1

0

我解决了在对象!之后添加 a 的问题。Place我想是为了告诉我我确定我在这个变量中有一个对象并且它不是零。

if let placeName = placemark["name"].element?.text {

    NSLog("Place name defined, object created")

    // Overwrite the place variable with a new object                
    place = Place(name: placeName, description: placemark["description"].element?.text, latitude: (coord![1] as NSString).doubleValue, longitude: (coord![0] as NSString).doubleValue)

    var distance = place!.getDistance(self.middlePosition)

    if distance < minDistance {
        minDistance = distance
        closestPlace = place!
    } else {
        NSLog("Place name could not be found, skipped")
    }
}
于 2014-10-21T23:27:48.440 回答