2014-10-20 64 views
1

我試圖構建我的第一個Swift應用程序。在這個應用程序中,我正在循環一個KML文件,其中包含有關某個餐廳的信息以及它們中的每一個我試圖用可用信息構建一個Place對象,比較距離並保持最接近於某個餐廳的Place給定點。未使用Xcode 6.1設置的Swift可選變量

這裏是我的地方的模型,一個非常簡單的模型(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") 
      } 
     } 
    } 

我在計算距離時在此腳本中添加了斷點。 place變量的值爲零,我不明白爲什麼。通過這條線

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) 

我可以看到我的地方對象是否正確現在實例化,但是我不明白爲什麼。 我也有相同的問題,當我試圖挽救最接近的地方:即使被設置與我的地方對象後

closestPlace = place 

在檢查的closestPlace的價值爲零。

+0

當你聲明可選項時,不要將它們賦給'nil'。只需聲明如下:'var value:Type?'。不知道解決問題,但實際上它是由編譯器完成的,所以你不需要它 – Pintouch 2014-10-21 13:55:06

+0

它不會。如果你閱讀文檔,'var value:Type?'default to nil value,我只需添加'= nil',這對每個人都是明確的 – maxwell2022 2014-10-21 23:22:44

回答

0

我解決了我的問題,在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") 
    } 
}