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

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

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

Firebaseは、Googleが提供するBasSサービスの一つ。リアルタイム通知可能、並びにアクセス制御ができるオブジェクトデータベース機能を備えます。さらに認証機能、アプリケーションのログ解析機能などの利用も可能です。

Swift

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

Q&A

解決済

1回答

563閲覧

Swift Realtime Database からデータの読み込み

globalplus

総合スコア119

Firebase

Firebaseは、Googleが提供するBasSサービスの一つ。リアルタイム通知可能、並びにアクセス制御ができるオブジェクトデータベース機能を備えます。さらに認証機能、アプリケーションのログ解析機能などの利用も可能です。

Swift

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

0グッド

0クリップ

投稿2019/11/25 18:40

編集2019/11/25 18:42

座標をDouble型としてRealtime Databaseからの読み込みができません。(読み込んだ値がコンソールでnilと表示されます)エラーもありません。どこが間違っているか教えて下さい。

viewcontroller

1class FirstViewController: UIViewController, CLLocationManagerDelegate { 2 3 @IBOutlet weak var mapView: MKMapView! 4 @IBOutlet weak var latitude: UILabel! 5 @IBOutlet weak var longitude: UILabel! 6 let user = Auth.auth().currentUser?.uid 7 let locationManager = CLLocationManager() 8 var ref: DatabaseReference! 9 var otherUserLatitude: Double? = nil 10 var otherUserLongitude: Double? = nil 11 12 override func viewDidLoad() { 13 super.viewDidLoad() 14 ref = Database.database().reference() 15 locationManager.delegate = self as! CLLocationManagerDelegate 16 } 17 //上手くいっている 18 @IBAction func userLocation(_ sender: Any) 19 { 20 let refLatitude = ref.child("userLatitude") 21 refLatitude.setValue(latitude.text) 22 let refLongitude = ref.child("userLongitude") 23 refLongitude.setValue(longitude.text) 24 print("位置情報を保存しました") 25 } 26 //DBから取得できていない 27 @IBAction func othersLocation(_ sender: Any) 28 { 29 ref.child("userLatitude").observeSingleEvent(of: .value, with: { (snapshot) in 30 let value = snapshot.value as? NSDictionary 31 let otherUserLatitude = value?["userLatitude"] as? Double 32 //コンソール表示otherUserLatitude: nil 33 print("otherUserLatitude:", otherUserLatitude) 34 }) 35 ref.child("userLongitude").observeSingleEvent(of: .value, with: { (snapshot) in 36 let value = snapshot.value as? NSDictionary 37 let otherUserLongitude = value?["userLongitude"] as? Double 38 //コンソール表示otherUserLongitude: nil 39 print("otherUserLongitude:", otherUserLongitude) 40 }) 41 } 42 func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { 43 switch status { 44 case .notDetermined: 45 locationManager.requestWhenInUseAuthorization() 46 case .authorizedWhenInUse: 47 locationManager.startUpdatingLocation() 48 default: 49 break 50 } 51 } 52 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 53 if let coordinate = locations.last?.coordinate { 54 // 現在地を拡大して表示する 55 let span = MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01) 56 let region = MKCoordinateRegion(center: coordinate, span: span) 57 mapView.region = region 58 } 59 guard let newLocation = locations.last else { 60 return 61 } 62 self.latitude.text = "".appendingFormat("%.4f", newLocation.coordinate.latitude) 63 self.longitude.text = "".appendingFormat("%.4f", newLocation.coordinate.longitude) 64 let location = locations.first 65 let latitude = location?.coordinate.latitude 66 let longitude = location?.coordinate.longitude 67 68 print("latitude: (latitude!)\nlongitude: (longitude!)") 69 } 70 71 72} 73 74

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

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

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

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

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

guest

回答1

0

ベストアンサー

文字列(String)として保存した値を数値(Double)で取り出そうとしているからです。
いづれかに合わせましょう。

投稿2019/11/26 00:29

MasakiHori

総合スコア3384

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

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

globalplus

2019/11/26 02:58

最初からDoubleで保存する方法を少し詳しく教えていただけませんか? IBActionで繋がったLabelはデフォルトでstringで保存されると思うのでどうやるかがわからないです。。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問