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

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

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

MacOSとは、Appleの開発していたGUI(グラフィカルユーザーインターフェース)を採用したオペレーションシステム(OS)です。Macintoshと共に、市場に出てGUIの普及に大きく貢献しました。

Xcode

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

Swift

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

Q&A

解決済

1回答

1887閲覧

[Xcode/Swift]逆GEOコードでのエラーで困っています

cafe_takai

総合スコア27

MacOS(OSX)

MacOSとは、Appleの開発していたGUI(グラフィカルユーザーインターフェース)を採用したオペレーションシステム(OS)です。Macintoshと共に、市場に出てGUIの普及に大きく貢献しました。

Xcode

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

Swift

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

0グッド

1クリップ

投稿2017/06/21 07:59

Xcode初心者です。緯度・経度から住所を得たく、こちらのサイトをそのままコピペしました。

そうしたところ、サイト情報との環境違い(Version 8.3.3)か、いくつかエラーが出ました。エラーは赤の中に白玉があったので、XcodeのFIXに従って次の通り修正しています。

swift

1import UIKit 2import MapKit 3 4class ViewController: UIViewController { 5 6 override func viewDidLoad() { 7 super.viewDidLoad() 8 ViewController.invoke() 9 } 10 11 override func didReceiveMemoryWarning() { 12 super.didReceiveMemoryWarning() 13 } 14 15 private static func invoke() { 16 17 let locations = [ 18 ["0,0", "0", "0"], 19 ["35,135", "35", "135"], 20 ["名古屋駅", "35.1707", "136.8826"], 21 ["名古屋城", "35.1856", "136.8991"], 22 ["木曽川", "35.2564", "136.6913"], 23 ["琵琶湖", "35.2676", "136.0937"], 24 ["インド洋", "-22.3918", "80.0957"], 25 ["スペリオル湖", "47.8247", "-87.3552"], 26 ["エッフェル塔", "48.858244", "2.294598"], 27 ] 28 29 for location in locations { 30 let latlon = CLLocation(latitude: Double(location[1])!, longitude: Double(location[2])!) 31 reverseGeocode(name: location[0], location: latlon, completion: printLocation) 32 } 33 } 34 35 private static func reverseGeocode(name: String, location: CLLocation, completion:@escaping (_ name: String, _ location: CLLocation, _ placemarks: [CLPlacemark]?, _ error: NSError?) -> Void) { 36 37 let geocorder = CLGeocoder() 38 39 geocorder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in 40 completion(name, location, placemarks, (error! as NSError)) 41 }) 42 43 } 44 45 private static func printLocation(name: String, location: CLLocation, placemarks: [CLPlacemark]?, error: NSError?) { 46 47 print("**********************************************************************") 48 print("[\(name)]") 49 print("latitude: \(location.coordinate.latitude)") 50 print("longitude: \(location.coordinate.longitude)") 51 52 if let ps = placemarks { 53 print("CLPlacemarks.count: \(ps.count)") 54 for p in ps { 55 print("CLPlacemark: [") 56 // The location object containing latitude and longitude information. 57 if p.location != nil { 58 print(" location: \(p.location!)") 59 } 60 // The name of the placemark. 61 if p.name != nil { 62 print(" name: \(p.name!)") 63 } 64 // A dictionary containing the Address Book keys and values for the placemark. 65 if let ad = p.addressDictionary { 66 print(" addressDictionary: [") 67 for k in ad.keys { 68 if let v = ad[k] { 69 print(" \(k): \(v) (\(type(of: v)))") 70 if let array = v as? NSMutableArray { 71 print(" [") 72 for elem in array { 73 print(" \(elem)") 74 } 75 print(" ]") 76 } 77 } 78 } 79 print(" ]") 80 } 81 // The abbreviated country name. 82 if p.isoCountryCode != nil { 83 print(" ISOcountryCode: \(p.isoCountryCode!)") 84 } 85 // The name of the country associated with the placemark. 86 if p.country != nil { 87 print(" country: \(p.country!)") 88 } 89 // The postal code associated with the placemark. 90 if p.postalCode != nil { 91 print(" postalCode: \(p.postalCode!)") 92 } 93 // The state or province associated with the placemark. 94 if p.administrativeArea != nil { 95 print(" administrativeArea: \(p.administrativeArea!)") 96 } 97 // Additional administrative area information for the placemark. 98 if p.subAdministrativeArea != nil { 99 print(" subAdministrativeArea: \(p.subAdministrativeArea!)") 100 } 101 // The city associated with the placemark. 102 if p.locality != nil { 103 print(" locality: \(p.locality!)") 104 } 105 // Additional city-level information for the placemark. 106 if p.subLocality != nil { 107 print(" subLocality: \(p.subLocality!)") 108 } 109 // The street address associated with the placemark. 110 if p.thoroughfare != nil { 111 print(" thoroughfare: \(p.thoroughfare!)") 112 } 113 // Additional street-level information for the placemark. 114 if p.subThoroughfare != nil { 115 print(" subThoroughfare: \(p.subThoroughfare!)") 116 } 117 // The geographic region associated with the placemark. 118 if p.region != nil { 119 print(" region: \(p.region!)") 120 } 121 // The time zone associated with the placemark. 122 if p.timeZone != nil { 123 print(" timeZone: \(p.timeZone!)") 124 } 125 // The name of the inland water body associated with the placemark. 126 if p.inlandWater != nil { 127 print(" inlandWater: \(p.inlandWater!)") 128 } 129 // The name of the ocean associated with the placemark. 130 if p.ocean != nil { 131 print(" ocean: \(p.ocean!)") 132 } 133 // The relevant areas of interest associated with the placemark. 134 if p.areasOfInterest != nil { 135 print(" areasOfInterest: \(p.areasOfInterest!)") 136 } 137 print("]") 138 } 139 } 140 141 if let e = error { 142 print("Error: \(e)") 143 } 144 } 145} 146

そのまま実行したところ、次のところでエラーが出ております。
イメージ説明

EXE_BAD...を頼りに、色々と調べ、またerrorにNILが入っているのではないかなど疑ったのですが、どうにも先に進めなくなっております。

どなた様かご教授賜りたく質問させていただく次第です。よろしくお願いいたします。

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

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

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

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

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

fromageblanc

2017/06/21 08:11

NSError を Error に変えるとどうなります?
cafe_takai

2017/06/22 07:21 編集

NSErrorをErrorに変えましたところ、実行はできるのですが、エラーとしては投稿させていただいたものと同様のようです。当初、Error Domain=kCLErrorDomain Code=2 "(null)"となっていたのですが、無線LANが切れていることに気づかず、投稿していしまいました。2つ目の投稿は無視していただければと思っております。
cafe_takai

2017/06/22 07:22 編集

本内容につきましては修正させていただきました。失礼いたしました。
guest

回答1

0

ベストアンサー

NSErrorの箇所をErrorにして、余計なキャストをやめると、私の環境では動きました。
Xcode 8.0 / Swift3.x

Swift

1import UIKit 2import MapKit 3 4class ViewController: UIViewController { 5 6 override func viewDidLoad() { 7 super.viewDidLoad() 8 ViewController.invoke() 9 } 10 11 override func didReceiveMemoryWarning() { 12 super.didReceiveMemoryWarning() 13 } 14 15 private static func invoke() { 16 17 let locations = [ 18 ["0,0", "0", "0"], 19 ["35,135", "35", "135"], 20 ["名古屋駅", "35.1707", "136.8826"], 21 ["名古屋城", "35.1856", "136.8991"], 22 ["木曽川", "35.2564", "136.6913"], 23 ["琵琶湖", "35.2676", "136.0937"], 24 ["インド洋", "-22.3918", "80.0957"], 25 ["スペリオル湖", "47.8247", "-87.3552"], 26 ["エッフェル塔", "48.858244", "2.294598"], 27 ] 28 29 for location in locations { 30 let latlon = CLLocation(latitude: Double(location[1])!, longitude: Double(location[2])!) 31 reverseGeocode(name: location[0], location: latlon, completion: printLocation) 32 } 33 } 34 35 private static func reverseGeocode(name: String, location: CLLocation, completion:@escaping (_ name: String, _ location: CLLocation, _ placemarks: [CLPlacemark]?, _ error: Error?) -> Void) { 36 37 let geocorder = CLGeocoder() 38 39 geocorder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in 40 completion(name, location, placemarks, (error)) 41 }) 42 43 } 44 45 private static func printLocation(name: String, location: CLLocation, placemarks: [CLPlacemark]?, error: Error?) { 46 47 print("**********************************************************************") 48 print("[\(name)]") 49 print("latitude: \(location.coordinate.latitude)") 50 print("longitude: \(location.coordinate.longitude)") 51 52 if let ps = placemarks { 53 print("CLPlacemarks.count: \(ps.count)") 54 for p in ps { 55 print("CLPlacemark: [") 56 // The location object containing latitude and longitude information. 57 if p.location != nil { 58 print(" location: \(p.location!)") 59 } 60 // The name of the placemark. 61 if p.name != nil { 62 print(" name: \(p.name!)") 63 } 64 // A dictionary containing the Address Book keys and values for the placemark. 65 if let ad = p.addressDictionary { 66 print(" addressDictionary: [") 67 for k in ad.keys { 68 if let v = ad[k] { 69 print(" \(k): \(v) (\(type(of: v)))") 70 if let array = v as? NSMutableArray { 71 print(" [") 72 for elem in array { 73 print(" \(elem)") 74 } 75 print(" ]") 76 } 77 } 78 } 79 print(" ]") 80 } 81 // The abbreviated country name. 82 if p.isoCountryCode != nil { 83 print(" ISOcountryCode: \(p.isoCountryCode!)") 84 } 85 // The name of the country associated with the placemark. 86 if p.country != nil { 87 print(" country: \(p.country!)") 88 } 89 // The postal code associated with the placemark. 90 if p.postalCode != nil { 91 print(" postalCode: \(p.postalCode!)") 92 } 93 // The state or province associated with the placemark. 94 if p.administrativeArea != nil { 95 print(" administrativeArea: \(p.administrativeArea!)") 96 } 97 // Additional administrative area information for the placemark. 98 if p.subAdministrativeArea != nil { 99 print(" subAdministrativeArea: \(p.subAdministrativeArea!)") 100 } 101 // The city associated with the placemark. 102 if p.locality != nil { 103 print(" locality: \(p.locality!)") 104 } 105 // Additional city-level information for the placemark. 106 if p.subLocality != nil { 107 print(" subLocality: \(p.subLocality!)") 108 } 109 // The street address associated with the placemark. 110 if p.thoroughfare != nil { 111 print(" thoroughfare: \(p.thoroughfare!)") 112 } 113 // Additional street-level information for the placemark. 114 if p.subThoroughfare != nil { 115 print(" subThoroughfare: \(p.subThoroughfare!)") 116 } 117 // The geographic region associated with the placemark. 118 if p.region != nil { 119 print(" region: \(p.region!)") 120 } 121 // The time zone associated with the placemark. 122 if p.timeZone != nil { 123 print(" timeZone: \(p.timeZone!)") 124 } 125 // The name of the inland water body associated with the placemark. 126 if p.inlandWater != nil { 127 print(" inlandWater: \(p.inlandWater!)") 128 } 129 // The name of the ocean associated with the placemark. 130 if p.ocean != nil { 131 print(" ocean: \(p.ocean!)") 132 } 133 // The relevant areas of interest associated with the placemark. 134 if p.areasOfInterest != nil { 135 print(" areasOfInterest: \(p.areasOfInterest!)") 136 } 137 print("]") 138 } 139 } 140 141 if let e = error { 142 print("Error: \(e)") 143 } 144 } 145} 146

投稿2017/06/21 08:30

fromageblanc

総合スコア2724

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

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

cafe_takai

2017/06/21 08:36

早速のご回答、ありがとうございます。 私の環境でも上手くいきました! NSError/Errorとキャストの件、勉強させていただきます<(_ _)>。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問