简体   繁体   中英

Swift optional variable not set using Xcode 6.1

I'm trying to build my first Swift application. In this application I'm looping over an KML file that contains information about some restaurant and for each one of them I'm trying to build a Place object with the available information, compare a distance and keep the Place which is the closest to a given point.

Here is my Place model, a very simple model (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)
    }
}

and here is the part of the application that is looping over the items from the KML file.

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

I added breakpoints in this script, when the distance is calculated. The value of the place variable is nil and I don't understand why. If I replace this line:

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

by this line:

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

I can see that my place object is instantiated correctly now but I can't understand why. Also I have the exact same issue when I tried to save the closest place:

closestPlace = place

In the inspector the value of closestPlace is nil even after being set with my place object.

I fixed my issue adding a ! after the Place object. I guess is to tell that I am sure I have an Object in this variable and that is not 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")
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM