Ios 未使用Xcode 6.1设置Swift可选变量
我正在尝试构建我的第一个Swift应用程序。在这个应用程序中,我在一个KML文件上循环,该文件包含有关某个餐厅的信息,对于每个餐厅,我试图用可用的信息构建一个Place对象,比较一个距离,并保持最接近给定点的位置 这是我的Place模型,一个非常简单的模型Place.swift:Ios 未使用Xcode 6.1设置Swift可选变量,ios,xcode,swift,optional-variables,Ios,Xcode,Swift,Optional Variables,我正在尝试构建我的第一个Swift应用程序。在这个应用程序中,我在一个KML文件上循环,该文件包含有关某个餐厅的信息,对于每个餐厅,我试图用可用的信息构建一个Place对象,比较一个距离,并保持最接近给定点的位置 这是我的Place模型,一个非常简单的模型Place.swift: import Foundation import MapKit class Place { var name:String var description:String? = nil var
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(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
在inspector中,closestPlace的值为零,即使在使用my 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")
}
}
当您声明选项时,不会将其指定为nil。只需这样声明:var-value:Type?。不确定这是否能解决问题,但事实上它是由编译器完成的,因此您不需要它。如果您阅读了文档,var值:Type?默认为nil值,我只添加了`=nil`,这样大家都清楚了