什么是致命错误:在展开可选值

时间:2017-08-21 06:33:43

标签: ios arrays swift

旧帖已经回答了这个问题。由于我是Swift的新手,一些声明对我来说真的很混乱,而且一些更新,一些结构也会发生变化。 现在我正在处理从数组记录到地图视图的这个错误。

当我打印这个课时,我得到了结果

class LocationModel: NSObject {
    //properties

    var name: String?
    var address: String?
    var latitude: String?
    var longitude: String?


    //empty constructor

    override init()
    {

    }

    //construct with @name, @address, @latitude, and @longitude parameters

    init(name: String, address: String, latitude: String, longitude: String) {

        self.name = name
        self.address = address
        self.latitude = latitude
        self.longitude = longitude

    }


    //prints object's current state

    override var description: String {
        return "Name: \(name), Address: \(address), Latitude: \(latitude), Longitude: \(longitude)"

    }


    }


but when I used it here, its showing nil. 

    class DetailViewController : UIViewController {

    @IBOutlet weak var mapView: MKMapView!

    var selectedLocation : LocationModel?

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewDidAppear(_ animated: Bool) {
        // Create coordinates from location lat/long
        var poiCoodinates: CLLocationCoordinate2D = CLLocationCoordinate2D()

        poiCoodinates.latitude = CDouble(self.selectedLocation!.latitude!)!
        poiCoodinates.longitude = CDouble(self.selectedLocation!.longitude!)!
        // Zoom to region
        let viewRegion: MKCoordinateRegion = MKCoordinateRegionMakeWithDistance(poiCoodinates, 750, 750)
        self.mapView.setRegion(viewRegion, animated: true)
        // Plot pin
        let pin: MKPointAnnotation = MKPointAnnotation()
        pin.coordinate = poiCoodinates
        self.mapView.addAnnotation(pin)

        //add title to the pin
        pin.title = selectedLocation!.name
    }

}
你能告诉我哪个陈述错了吗? 附:我真的很新,所以我想要了解每一个声明。

0 个答案:

没有答案