2015-02-09 23 views
1

不知道爲什麼這不工作?任何幫助將不勝感激。 Thnks。爲什麼我得到致命錯誤:意外地發現零,而解包可選值...這裏是我的代碼:

import UIKit 
import MapKit 
import CoreLocation 

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate { 

    var locationManager:CLLocationManager! 
    @IBOutlet weak var mapView: MKMapView! 

    @IBAction func nowButton(sender: AnyObject) { 
    } 

    @IBOutlet weak var mapLabel: UILabel! 

    @IBAction func bookButton(sender: AnyObject) {  
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     locationManager = CLLocationManager() 
     locationManager.requestAlwaysAuthorization()   
     locationManager.desiredAccuracy = kCLLocationAccuracyBest 
     locationManager.delegate = self 
     locationManager.startUpdatingLocation() 

     mapView.showsUserLocation = true 
     mapView.delegate = self 

     // Do any additional setup after loading the view, typically from a nib. 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) { 
     let regionToZoom = MKCoordinateRegionMake(manager.location.coordinate, MKCoordinateSpanMake(0.05, 0.05)) 
     mapView.setRegion(regionToZoom, animated: true) 
    } 

} 
+1

哪條線給你錯誤? – 2015-02-09 07:27:46

+0

除了上面所有的'IBOutlet's連接? – Rich 2015-02-09 07:37:34

回答

0

可能的故障原因在於,因爲這條線在didUpdateLocations委託方法中:

let regionToZoom = MKCoordinateRegionMake(manager.location.coordinate, MKCoordinateSpanMake(0.05, 0.05)) 

你需要像下面行創建區域(替換上面一行此行):

let regionToZoom = MKCoordinateRegion(center: manager.location.coordinate, span: MKCoordinateSpanMake(0.05, 0.05))); 
相關問題