【问题标题】:Swift optional variable not set using Xcode 6.1Swift 可选变量未使用 Xcode 6.1 设置
【发布时间】:2014-10-22 07:27:48
【问题描述】:

我正在尝试构建我的第一个 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")
            }
        }
    }

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

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。

【问题讨论】:

  • 当你声明可选项时,不要将它们分配给nil。像这样声明它:var value:Type?。不确定是否能解决问题,但实际上它是由编译器完成的,所以你不需要它
  • 不会的。如果您阅读文档,var value:Type? 默认为 nil 值,我只需添加 `=nil` 以便所有人都清楚

标签: ios xcode swift optional-variables


【解决方案1】:

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

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")
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多