前提・実現したいこと
Swiftでグルメアプリを製作中なのですが、エラーがなかなか解決できずかなりつまづいています。解決法やアドバイスなど頂けると幸いです。
やりたいこと
FirebaseのFirestoreから取得したデータを元に、CoreLocationを用いて現在地から近い順にデータを並べ替え、TableViewに表示したい。
今やろうとしていることは、CoreLocationを用いて現在地と店舗との距離を測定し、keyをDistanceとしてデータに追加しようとしています。しかし下記のエラーが出てしまい、うまくいきません。
まずこの方法で現在地から近い順にソートできるかも自身もないため、そのほかにもやり方などあれば教えていただきたいです。
発生している問題・エラーメッセージ
Type 'Shop' has no subscript members
該当のソースコード
shopArrayにShop型のデータが突っ込んであります。
Swift
1 func locationManager(_ manager: CLLocationManager, didUpdateLOcations locations: [CLLocation]){ 2 let localValue:CLLocationCoordinate2D = manager.location!.coordinate 3 print("locations =(localValue.latitude) (localValue.longitude)") 4 5 for i in 0 ..< self.shopArray.count{ 6 let currentLocation : CLLocation = CLLocation(latitude: localValue.latitude, longitude: localValue.longitude) 7 let shopLocation : CLLocation = CLLocation(latitude: self.shopArray[i].latitude, longitude: self.shopArray[i].longitude) 8 let distance = shopLocation.distance(from: currentLocation) 9 shopArray[i]["distance"] = "distance"#エラーはこの行に出ます。 10 11データの形式は下記です。 12struct Shop { 13 var title: String 14 var recomendation: String 15 var tel: String 16 var station: String 17 var latitude: Double 18 var longitude: Double 19 var price: Int 20 var img: String 21 var distance: CLLocationDistance? 22 23 24 25 26 } 27追記 28 29様々な回答を参考に修正したのですが、distanceがnilになってしまいます。shopArrayにデータを格納するコードの方を追記致しましたので、どの点に間違いがあるかご教授くださると幸いです。 30 31 32shopArrayにデータを格納するコード 33 34 func loadData() { 35 let db = Firestore.firestore() 36 let shopsRef = db.collection("shops") 37 38 shopsRef.getDocuments() { (snapshot, err) in 39 if let err = err { 40 print("Error getting documents: (err)") 41 } else { 42 if let snapshot = snapshot{ 43 for document in snapshot.documents { 44 45 print(document.data()) 46 let data = document.data() 47 let title = data["title"] as? String ?? "" 48 let recomendation = data["recomendation"] as? String ?? "" 49 let tel = data["tel"] as? String ?? "" 50 let station = data["station"] as? String ?? "" 51 let latitude = data["latitude"] as? Double ?? Double() 52 let longitude = data["longitude"] as? Double ?? Double() 53 let price = data["price"] as? Int ?? Int() 54 let img = data["img"] as? String ?? "" 55 let distance = data["distance"] as? CLLocationDistance ?? Double() 56 57 let newShop = Shop(title: title, recomendation: recomendation, tel: tel, station: station, latitude: latitude, longitude: longitude, price: price, img: img, distance: distance) 58 self.shopArray.append(newShop) 59 60 func locationManager(_ manager: CLLocationManager, didUpdateLOcations locations: [CLLocation]){ 61 guard let currentLocation = manager.location else { 62 print("location is nil") 63 return 64 } 65 print("locations =(currentLocation.coordinate)") 66 67 68 for i in self.shopArray.indices{ 69 // let currentLocation : CLLocation = CLLocation(latitude: localValue.latitude, longitude: localValue.longitude) 70 let shopLocation : CLLocation = CLLocation(latitude: self.shopArray[i].latitude, longitude: self.shopArray[i].longitude) 71 let distance = shopLocation.distance(from: currentLocation) 72 self.shopArray[i].distance = distance //<- 73 74 } 75 self.shopArray.sort{$0.distance ?? 0.0 < $1.distance ?? 0.0} 76 self.cafeTableView.reloadData() 77 } 78 79 80 } 81 82 83 } 84 85 } 86 87 88 89 } 90 }

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/09/01 09:47