2016-02-18 81 views
-5

請保留此代碼。我是Swift新手(今天才開始)。這裏有什麼問題?我嘗試搜索堆棧,但只找到Objective-C解決方案。Swift錯誤EXC_BAD_INSTRUCTION

class ViewController: UIViewController, CLLocationManagerDelegate { 
var locationManager = CLLocationManager() 

@IBOutlet weak var mapView: MKMapView! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    locationManager.delegate = self 

    if (CLLocationManager.locationServicesEnabled()) { 
     locationManager = CLLocationManager() 
     locationManager.delegate = self 
     locationManager.desiredAccuracy = kCLLocationAccuracyBest 
     locationManager.requestAlwaysAuthorization() 
     locationManager.startUpdatingLocation() 
    } 
} 

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) 
{ 
    let location = locations.last! as CLLocation 

    let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude) 
    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)) 

    mapView.setRegion(region, animated: true) 
} 

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

錯誤是哪一行?什麼都記錄到控制檯? – NobodyNada

+0

'mapView.setRegion(region,animated:true)'this line – melol

回答

1

你忘了在故事板地圖視圖鉤在代碼中你mapView出口,大概。

+0

順便說一句,如果你只想讓地圖視圖跟蹤用戶的位置,你所做的是不是做它的方式。 – matt

+0

你的意思是拖放? – melol

+0

您是否有任何洞見,爲什麼蘋果公司沒有把這個編譯錯誤?什麼時候有人會從沒有連接插座中受益?它會阻止每天都會問這個問題。 – Caleb

相關問題