質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Realm

RealmとはSQLiteやCore Dataに代わるモバイルデータベースです。iOSとAndroidの両方でサポートされています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

2125閲覧

RealmSwiftを用いたSwift3でのGPSログでの"Thread 1:signal SIGABRT"について

TaigaMikami

総合スコア20

Realm

RealmとはSQLiteやCore Dataに代わるモバイルデータベースです。iOSとAndroidの両方でサポートされています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2017/05/03 09:49

###前提・実現したいこと
アプリケーションの実装

###発生している問題・エラーメッセージ
RealmSwiftでGPSロガーを作成しようとしているのですが、"Thread 1:signal SIGABRT"が出てきてエラーの原因がわかりません。Storyboardでのコンポーネントの接続もできています。
以下の部分まではデバックできるのですが、、、
イメージ説明
ここをすぎるとAppDelegate.swiftでこのようになってしまいます。
イメージ説明

このサイトを参考にさせていただきました。→参考サイト
ほとんど写経しただけです。

import UIKit import MapKit import RealmSwift class Location: Object { dynamic var latitude: Double = 0.0 dynamic var longitude: Double = 0.0 dynamic var createdAt = Date(timeIntervalSince1970: 1) } class ViewController: UIViewController, CLLocationManagerDelegate { @IBOutlet weak var mapView: MKMapView! @IBOutlet weak var startButton: UIButton! @IBOutlet weak var tableView: UITableView! var locationManager: CLLocationManager! var locations: Results<Location>! var token: NotificationToken! var isUpdating = false override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. self.locationManager = CLLocationManager() self.locationManager.delegate = self self.locationManager.desiredAccuracy = kCLLocationAccuracyBest self.locationManager.distanceFilter = 100 // Delete old location objects self.deleteOldLocations() // Load stored location objects self.locations = self.loadStoredLocations() // Drop pins for location in self.locations { self.dropPin(at: location) } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // MARK: - Private methods @IBAction func startButtonDidTap(_ sender: AnyObject) { self.toggleLocationUpdate() } @IBAction func clearButtonDidTap(_ sender: AnyObject) { self.deleteAllLocations() self.locations = self.loadStoredLocations() self.removeAllAnnotations() self.tableView.reloadData() } // Load locations stored in realm at the table view fileprivate func loadStoredLocations() -> Results<Location> { // Get the default Realm let realm = try! Realm() // Load recent location objects return realm.objects(Location.self).sorted(byKeyPath: "createdAt", ascending: false) } // Start or Stop location update fileprivate func toggleLocationUpdate() { let realm = try! Realm() if self.isUpdating { // Stop self.isUpdating = false self.locationManager.stopUpdatingLocation() self.startButton.setTitle("Start", for: UIControlState()) // Remove a previously registered notification if let token = self.token { token.stop() } } else { // Start self.isUpdating = true self.locationManager.startUpdatingLocation() self.startButton.setTitle("Stop", for: UIControlState()) // Add a notification handler for changes self.token = realm.addNotificationBlock { [weak self] notification, realm in self?.tableView.reloadData() } } } // Store object fileprivate func addCurrentLocation(_ rowLocation: CLLocation) { let location = makeLocation(rawLocation: rowLocation) // Get the default Realm let realm = try! Realm() // Add to the Realm inside a transaction try! realm.write { realm.add(location) } } // Delete old (-1 day) objects in a background thread fileprivate func deleteOldLocations() { DispatchQueue.global().async { // Get the default Realm let realm = try! Realm() // Old Locations stored in Realm let oldLocations = realm.objects(Location.self).filter(NSPredicate(format:"createdAt < %@", NSDate().addingTimeInterval(-86400))) // Delete an object with a transaction try! realm.write { realm.delete(oldLocations) } } } // Delete all location objects from realm fileprivate func deleteAllLocations() { // Get the default Realm let realm = try! Realm() // Delete all objects from the realm try! realm.write { realm.deleteAll() } } // Make Location object from CLLocation fileprivate func makeLocation(rawLocation: CLLocation) -> Location { let location = Location() location.latitude = rawLocation.coordinate.latitude location.longitude = rawLocation.coordinate.longitude location.createdAt = Date() return location } // Drop pin on the map fileprivate func dropPin(at location: Location) { if location.latitude != 0 && location.longitude != 0 { let annotation = MKPointAnnotation() annotation.coordinate = CLLocationCoordinate2DMake(location.latitude, location.longitude) annotation.title = "\(location.latitude),\(location.longitude)" annotation.subtitle = location.createdAt.description self.mapView.addAnnotation(annotation) } } // Remove all pins on the map fileprivate func removeAllAnnotations() { let annotations = self.mapView.annotations.filter { $0 !== self.mapView.userLocation } self.mapView.removeAnnotations(annotations) } // MARK: - CLLocationManager delegate func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { if status == CLAuthorizationStatus.notDetermined { locationManager.requestAlwaysAuthorization() } else if status == CLAuthorizationStatus.authorizedAlways { // Center user location on the map let span = MKCoordinateSpanMake(0.003, 0.003) let region = MKCoordinateRegionMake(self.mapView.userLocation.coordinate, span) self.mapView.setRegion(region, animated:true) self.mapView.userTrackingMode = MKUserTrackingMode.followWithHeading } } func locationManager(_ manager: CLLocationManager, didUpdateLocations locations:[CLLocation]) { guard let newLocation = locations.last else { return } if !CLLocationCoordinate2DIsValid(newLocation.coordinate) { return } self.addCurrentLocation(newLocation) let location = makeLocation(rawLocation: newLocation) dropPin(at: location) } // MARK: - MKMapView delegate func mapView(_ mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { if annotation is MKUserLocation { return nil } let reuseId = "annotationIdentifier" var pinView = self.mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView if pinView == nil { pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId) pinView?.canShowCallout = true pinView?.animatesDrop = true } else { pinView?.annotation = annotation } return pinView } // MARK: - Table view data source func numberOfSectionsInTableView(_ tableView: UITableView) -> Int { // Return the number of sections. return 1 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // Return the number of rows in the section. return self.locations.count } func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "CellIdentifier", for: indexPath) let location = self.locations[indexPath.row] cell.textLabel?.text = "\(location.latitude),\(location.longitude)" cell.detailTextLabel?.text = location.createdAt.description return cell } // MARK: - Table view delegate func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) { tableView.deselectRow(at: indexPath, animated: true) } }

###補足情報(言語/FW/ツール等のバージョンなど)
Swift3,Xcode8.3.1,シュミレータでの実装

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

CellのIdentifierにCellIdentifierを設定しているようですがこちらtypoなどありませんか?

下記の手順で確認してみてください^^/

①Storyboardでcellを選択
②Attribute Inspectorを開く
③Table View CellのIdentifierの部分(下図でCellと入力されてる部分)をチェック

イメージ説明

投稿2017/05/03 16:58

jollyjoester

総合スコア1585

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

TaigaMikami

2017/05/04 14:19

回答ありがとうございます。 確認してみると、まさしくCellのIdentifierが設定されていませんでした。 設定した後にデバッグしてみるとできず、、、しかし、CellのスタイルをSubtitleに変更するとできました!!これはどうしてでしょうか? お時間あれば御教授ください。 本当に回答ありがとうございます。
jollyjoester

2017/05/05 16:10

Cellのスタイルは関係ないはずなので保存できてなかったとかなのかなぁと思います。 もう一度お試しください!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問