前提・実現したいこと
Swift初学者です。
SwiftとMapkitを用いた、現在地をピンでつなげていく、軌跡プログラムをつくりたいと思っています。
そして、シュミレーターのFreeway driveをできるようにしたいと思ってます。
Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
というメッセージが出てしまい、コンパイルはできるものの、アプリがクラッシュしてしまいます。
教えていただきたいです。
よろしくお願い致します。
発生している問題・エラーメッセージ
Swift
1エラーメッセージ 2fatal error: unexpectedly found nil while unwrapping an Optional value 3
該当のソースコード
Swift
1 //最初からあるメソッド 2 override func viewDidLoad() { 3 super.viewDidLoad() 4 5 //デリゲート先に自分を設定する。 6 testManager.delegate = self 7 8 //位置情報の取得を開始する。 9 testManager.startUpdatingLocation() 10 11 //位置情報の利用許可を変更する画面をポップアップ表示する。 12 testManager.requestWhenInUseAuthorization() 13 14 //デリゲート先を自分に設定する。 15 testMapView.delegate = self 16 //↑ここでエラーメッセージ 17 18 } 19 20 21 22 //位置情報取得時の呼び出しメソッド 23 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 24 25 for location in locations { 26 27 //中心座標 28 let center = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude) 29 30 //表示範囲 31 let span = MKCoordinateSpanMake(0.01, 0.01) 32 33 //中心座標と表示範囲をマップに登録する。 34 let region = MKCoordinateRegionMake(center, span) 35 testMapView.setRegion(region, animated:true) 36 37 if(annotation == nil) { 38 //初回はマップにピンを格納する。 39 annotation = MKPointAnnotation() 40 annotation.coordinate = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude) 41 testMapView.addAnnotation(annotation) 42 } else { 43 //2回目以降は移動前と後の座標間に直線を引く。 44 45 //始点と終点の座標 46 var lineLocation:[CLLocationCoordinate2D] = [CLLocationCoordinate2D(latitude: annotation.coordinate.latitude, longitude: annotation.coordinate.longitude), 47 CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)] 48 49 //2点間に直線を描画する。 50 let line = MKPolyline(coordinates: &lineLocation, count: 2) 51 testMapView.add(line) 52 53 //ピンの位置を更新する。 54 annotation.coordinate = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude) 55 } 56 57 } 58 } 59 60
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
Swift 3.0.2
Xcode 8.2.1
コード全容を記述します。
Swift
1import UIKit 2import MapKit 3 4class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate { 5 6 @IBOutlet weak var testMapView: MKMapView! 7 8 var testManager:CLLocationManager = CLLocationManager() 9 10 //アノテーション 11 var annotation:MKPointAnnotation! 12 13 //最初からあるメソッド 14 override func viewDidLoad() { 15 super.viewDidLoad() 16 17 //デリゲート先に自分を設定する。 18 testManager.delegate = self 19 20 //位置情報の取得を開始する。 21 testManager.startUpdatingLocation() 22 23 //位置情報の利用許可を変更する画面をポップアップ表示する。 24 testManager.requestWhenInUseAuthorization() 25 26 //デリゲート先を自分に設定する。 27 testMapView.delegate = self 28 29 } 30 31 32 33 //位置情報取得時の呼び出しメソッド 34 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 35 36 for location in locations { 37 38 //中心座標 39 let center = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude) 40 41 //表示範囲 42 let span = MKCoordinateSpanMake(0.01, 0.01) 43 44 //中心座標と表示範囲をマップに登録する。 45 let region = MKCoordinateRegionMake(center, span) 46 testMapView.setRegion(region, animated:true) 47 48 if(annotation == nil) { 49 //初回はマップにピンを格納する。 50 annotation = MKPointAnnotation() 51 annotation.coordinate = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude) 52 testMapView.addAnnotation(annotation) 53 } else { 54 //2回目以降は移動前と後の座標間に直線を引く。 55 56 //始点と終点の座標 57 var lineLocation:[CLLocationCoordinate2D] = [CLLocationCoordinate2D(latitude: annotation.coordinate.latitude, longitude: annotation.coordinate.longitude), 58 CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)] 59 60 //2点間に直線を描画する。 61 let line = MKPolyline(coordinates: &lineLocation, count: 2) 62 testMapView.add(line) 63 64 //ピンの位置を更新する。 65 annotation.coordinate = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude) 66 } 67 68 } 69 } 70 71 72 73 //描画メソッド実行時の呼び出しメソッド 74 func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer { 75 let testRender = MKPolylineRenderer(overlay: overlay) 76 77 //直線の幅を設定する。 78 testRender.lineWidth = 3 79 80 //直線の色を設定する。 81 testRender.strokeColor = UIColor.red 82 83 return testRender 84 } 85} 86 87
試したこと
swift
1import UIKit 2import MapKit 3 4class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate { 5 6 @IBOutlet weak var testMapView: MKMapView? 7 8 var testManager:CLLocationManager = CLLocationManager() 9 10 //アノテーション 11 var annotation:MKPointAnnotation! 12 13 14 15 //最初からあるメソッド 16 override func viewDidLoad() { 17 super.viewDidLoad() 18 19 20 21 //デリゲート先に自分を設定する。 22 testManager.delegate = self 23 24 //位置情報の取得を開始する。 25 testManager.startUpdatingLocation() 26 27 //位置情報の利用許可を変更する画面をポップアップ表示する。 28 testManager.requestWhenInUseAuthorization() 29 30 print("testMapView:", testMapView) 31 32 //デリゲート先を自分に設定する。 33 testMapView?.delegate = self 34 35 } 36 37 38 39 //位置情報取得時の呼び出しメソッド 40 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 41 42 for location in locations { 43 44 //中心座標 45 let center = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude) 46 47 //表示範囲 48 let span = MKCoordinateSpanMake(0.01, 0.01) 49 50 //中心座標と表示範囲をマップに登録する。 51 let region = MKCoordinateRegionMake(center, span) 52 testMapView?.setRegion(region, animated:true) 53 54 if(annotation == nil) { 55 //初回はマップにピンを格納する。 56 annotation = MKPointAnnotation() 57 annotation.coordinate = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude) 58 testMapView?.addAnnotation(annotation) 59 } else { 60 //2回目以降は移動前と後の座標間に直線を引く。 61 62 //始点と終点の座標 63 var lineLocation:[CLLocationCoordinate2D] = [CLLocationCoordinate2D(latitude: annotation.coordinate.latitude, longitude: annotation.coordinate.longitude), 64 CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)] 65 66 //2点間に直線を描画する。 67 let line = MKPolyline(coordinates: &lineLocation, count: 2) 68 testMapView?.add(line) 69 70 //ピンの位置を更新する。 71 annotation.coordinate = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude) 72 } 73 74 } 75 } 76 77 78 79 //描画メソッド実行時の呼び出しメソッド 80 func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer { 81 let testRender = MKPolylineRenderer(overlay: overlay) 82 83 //直線の幅を設定する。 84 testRender.lineWidth = 3 85 86 //直線の色を設定する。 87 testRender.strokeColor = UIColor.red 88 89 return testRender 90 } 91} 92 93 94
「testMapView.delegate = self」でアプリがクラッシュしているのは確かですか??エラーメッセージの内容からnilが許容されない箇所にnilが渡されているためアプリがクラッシュしているかと思うのですが、「testMapView.delegate = self」ではおきなそうだなと。

回答1件
あなたの回答
tips
プレビュー