我正在制作一款使用地图的iPhone应用程序。我让应用程序通过故事板工作,但我决定转而以编程方式编写应用程序,这样我才能真正感觉到我正在学习我正在编写的代码。但是,我现在无法运行我的应用程序,我错误地发现了“致命错误:在解开可选值时意外发现了nil”。
我的ViewController看起来像这样:
import Foundation
import UIKit
import MapKit
class GoOut: UIViewController {
@IBOutlet var mapView: MKMapView!
let regionRadius: CLLocationDistance = 1000
let APP_ID = "5738E396-74BB-E1BD-FF13-C6916832AA00"
let SECRET_KEY = "BA71250D-582B-0E65-FF9A-35CAD17AF800"
let VERSION_NUM = "v1"
var pin_array: [[Double]] = []
var backendless = Backendless.sharedInstance()
var pressed = false
var date: NSDate? = nil
override func viewDidLoad() {
super.viewDidLoad()
let longPressRecogniser = UILongPressGestureRecognizer(target: self, action: "handleLongPress:")
longPressRecogniser.minimumPressDuration = 0.5
mapView.addGestureRecognizer(longPressRecogniser)
mapView.delegate = self
let initialLocation = CLLocation(latitude: 42.447918, longitude: -76.487844)
centerMapOnLocation(initialLocation)
backendless.initApp(APP_ID, secret:SECRET_KEY, version:VERSION_NUM)
backendless.hostURL = "http://api.backendless.com"
getMapPins()
var pin_number = 0
for element in pin_array {
let pin = Pin(title: String(pin_number),
coordinate:CLLocationCoordinate2D(latitude: element[0], longitude: element[1]))
mapView.addAnnotation(pin)
pin_number++
}
}
我有这个类的ViewController扩展,看起来像:
import Foundation
import MapKit
extension GoOut: MKMapViewDelegate {
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if let annotation = annotation as? Pin {
let identifier = "pin"
var view: MKPinAnnotationView
if let dequeuedView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
as? MKPinAnnotationView { // 2
dequeuedView.annotation = annotation
view = dequeuedView
} else {
view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
view.canShowCallout = false
view.animatesDrop = true
}
return view
}
return nil
}
}
当我尝试在视图控制器中修改mapView时,我的错误就出现了。有没有人有任何建议?
编辑:我添加了
mapView = MKMapView()
到我的视图控制器,它允许应用程序运行,但没有显示地图。