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

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

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

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

Q&A

解決済

2回答

1177閲覧

Swift 逆ジオコーディング(エラー内容はアンラップ)

globalplus

総合スコア119

Swift

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

0グッド

0クリップ

投稿2019/11/20 08:40

編集2019/11/21 18:59

取得した座標から逆ジオコーディングしてローケーションマークをおきたいです。
以下のエラーで困っています。アドバイスお願いします。

viewcontroller

1import UIKit 2import MapKit 3import CoreLocation 4 5class FirstViewController: UIViewController, CLLocationManagerDelegate { 6 7 @IBOutlet weak var mapView: MKMapView! 8 let locationManager = CLLocationManager() 9 let geocoder = CLGeocoder() 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 locationManager.delegate = self as! CLLocationManagerDelegate 14 } 15 @IBAction func othersLocation(_ sender: Any) 16 { 17 ref.child("userLatitude").observeSingleEvent(of: .value, with: { (snapshot) in 18 //DBから座標を取得 19 let value = snapshot.value as? NSDictionary 20 let otherUserLatitude = value?["userLatitude"] as? Double 21 let otherUserLongitude = value?["userLongitude"] as? Double 22 print("位置情報を取得しました") 23 //DBから取得した座標を用いて逆ジオコーディング 24 //otherUserLatitudeとotherUserLongitudeにValue of optional type 'Double?' must be unwrapped to a value of type 'Double'のエラー 25 let location = CLLocation(latitude: otherUserLatitude, longitude: otherUserLongitude) 26 geocoder.reverseGeocodeLocation(location) { (placemarks, error) 27 if let placemarks = placemarks { 28 if let pm = placemarks.first { 29 print("country: (pm.country ?? "")") 30 31 if let region = pm.region { 32 print("region: (region)") 33 } 34 if let timeZone = pm.timeZone { 35 print("timeZone: (timeZone)") 36 } 37 print("inlandWater: (pm.inlandWater ?? "")") 38 print("ocean: (pm.ocean ?? "")") 39 if let areasOfInterest = pm.areasOfInterest { 40 print("areasOfInterest: (areasOfInterest)") 41 } 42 } 43 } 44 } 45 }) 46 { (error) in 47 print(error.localizedDescription) 48 } 49 50} 51

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

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

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

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

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

stdio

2019/11/20 08:50 編集

すみません。プログラムのどこで困っているのか記載して下さい。 print文の数を増やして、どこでエラーが出ているのか分かれば解決も早いと思います。
globalplus

2019/11/20 08:50

otherUserLatitudeとotherUserLongitudeに Value of optional type 'Double?' must be unwrapped to a value of type 'Double'のエラーです。確認しづらくてすみません。
hameji

2019/11/21 02:43

ちなみにですが、、、 逆ジオコーディングのみのサンプルは作って動かしてみましたか? 一度に全部を接続して、動かないってなるよりは小分けに試して、 それから合わせるのがセオリーですよ。 その方が問題箇所の把握にも役立ちますし、 遠回りに見えて、近道なはずです。 今回の箇所も逆ジオコーディングが問題という訳ではないですし、、、
globalplus

2019/11/21 18:58

今回は一応小分けにしてジオコーディングだけやってみてから自分のプロジェクトに取り組んだのですが、アンラップがわからず質問させて頂きました。。 小分けにぜずやってしまう時もあるのでこれからは徹底しようと思います。。 いつもアドバイスありがとうございます。
guest

回答2

0

ベストアンサー

Value of optional type 'Double?' must be unwrapped to a value of type 'Double'

「オプションタイプ「Double?」の値 タイプ 'Double'の値にアンラップする必要があります」
と怒られていますね。アンラップしましょう。

投稿2019/11/20 08:52

stdio

総合スコア3307

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

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

globalplus

2019/11/20 18:32

オプショナル型について調べたところ、 var num: Int? num = 100 print("num:(num!)") //実行結果num:100 と言う例を見つけましたが、 アンラップするためのnum = 100 に該当する部分を自分のコード上でどの様に書いたら良いかわかりません。
stdio

2019/11/21 00:42

普通に「swift アンラップ」で調べると色々出ますよ。 例としては、下記のような方法があります。 ``` let hoge:String? = "Hello" if let hoge2 = hoge { print(hoge2) } ```
globalplus

2019/11/21 18:52

if let otherUserLatitude = otherUserLatitude { if let otherUserLongitude = otherUserLongitude { } } 素人くさいですが2つの定数で囲んだらエラーは消えました。ありがとうございます。
guest

0

//otherUserLatitudeとotherUserLongitudeにValue of optional type 'Double?' must be unwrapped to a value of type 'Double'のエラー

let location = CLLocation(latitude: otherUserLatitude, longitude: otherUserLongitude)

オプショナル型のままでは渡せないのでアンラップしてください。

実行時エラーを気にしないのであれば以下のコードでいけると思います。

let location = CLLocation(latitude: otherUserLatitude!, longitude: otherUserLongitude!)

投稿2019/11/20 08:51

takabosoft

総合スコア8356

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

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

globalplus

2019/11/20 18:13

!を付けたら Contextual type for closure argument list expects 2 arguments, which cannot be implicitly ignored とエラーが出たのですが翻訳すると クロージャー引数リストのコンテキスト型には2つの引数が必要ですが、暗黙的に無視することはできません 何をいってるのかわからないので教えて頂けませんか?
takabosoft

2019/11/21 01:18

geocoder.reverseGeocodeLocation(location) { (placemarks, error) ↓ geocoder.reverseGeocodeLocation(location) { (placemarks, error) in のようにinが抜けているのでは? あと、後ろの方の4行がゴミかもしれません。 }) { (error) in print(error.localizedDescription) } とにかく、クロージャの構文が間違っている系のエラーです。
globalplus

2019/11/21 18:56

確かにinがないとエラーが出ていました。 アンラップした事で質問にあったエラーが消えた為今回のベストアンサーはstdioさんにさせて頂きます。 takanosoftさんのアドバイスで追加で出たエラーも消えたのでお二人にベストアンサーを出したいのですが御了承下さい。。
takabosoft

2019/11/22 01:59

ベストアンサーはともかく、私が提示したビックリマークを付ける方法もアンラップの一つですよ?Forced Unwrappingというやつです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問