質問編集履歴

5

誤字です

2018/09/01 09:59

投稿

rhcp56
rhcp56

スコア8

test CHANGED
File without changes
test CHANGED
@@ -94,7 +94,17 @@
94
94
 
95
95
  }
96
96
 
97
-
97
+ 追記 
98
+
99
+
100
+
101
+ 様々な回答を参考に修正したのですが、distanceがnilになってしまいます。shopArrayにデータを格納するコードの方を追記致しましたので、どの点に間違いがあるかご教授くださると幸いです。
102
+
103
+
104
+
105
+
106
+
107
+ shopArrayにデータを格納するコード
98
108
 
99
109
 
100
110
 

4

修正

2018/09/01 09:59

投稿

rhcp56
rhcp56

スコア8

test CHANGED
File without changes
test CHANGED
@@ -94,122 +94,122 @@
94
94
 
95
95
  }
96
96
 
97
+
98
+
99
+
100
+
101
+ func loadData() {
102
+
103
+ let db = Firestore.firestore()
104
+
105
+ let shopsRef = db.collection("shops")
106
+
107
+
108
+
109
+ shopsRef.getDocuments() { (snapshot, err) in
110
+
111
+ if let err = err {
112
+
113
+ print("Error getting documents: (err)")
114
+
115
+ } else {
116
+
117
+ if let snapshot = snapshot{
118
+
119
+ for document in snapshot.documents {
120
+
121
+
122
+
123
+ print(document.data())
124
+
125
+ let data = document.data()
126
+
127
+ let title = data["title"] as? String ?? ""
128
+
129
+ let recomendation = data["recomendation"] as? String ?? ""
130
+
131
+ let tel = data["tel"] as? String ?? ""
132
+
133
+ let station = data["station"] as? String ?? ""
134
+
135
+ let latitude = data["latitude"] as? Double ?? Double()
136
+
137
+ let longitude = data["longitude"] as? Double ?? Double()
138
+
139
+ let price = data["price"] as? Int ?? Int()
140
+
141
+ let img = data["img"] as? String ?? ""
142
+
143
+ let distance = data["distance"] as? CLLocationDistance ?? Double()
144
+
145
+
146
+
147
+ let newShop = Shop(title: title, recomendation: recomendation, tel: tel, station: station, latitude: latitude, longitude: longitude, price: price, img: img, distance: distance)
148
+
149
+ self.shopArray.append(newShop)
150
+
151
+
152
+
153
+ func locationManager(_ manager: CLLocationManager, didUpdateLOcations locations: [CLLocation]){
154
+
155
+ guard let currentLocation = manager.location else {
156
+
157
+ print("location is nil")
158
+
159
+ return
160
+
161
+ }
162
+
163
+ print("locations =(currentLocation.coordinate)")
164
+
165
+
166
+
167
+
168
+
169
+ for i in self.shopArray.indices{
170
+
171
+ // let currentLocation : CLLocation = CLLocation(latitude: localValue.latitude, longitude: localValue.longitude)
172
+
173
+ let shopLocation : CLLocation = CLLocation(latitude: self.shopArray[i].latitude, longitude: self.shopArray[i].longitude)
174
+
175
+ let distance = shopLocation.distance(from: currentLocation)
176
+
177
+ self.shopArray[i].distance = distance //<-
178
+
179
+
180
+
181
+ }
182
+
183
+ self.shopArray.sort{$0.distance ?? 0.0 < $1.distance ?? 0.0}
184
+
185
+ self.cafeTableView.reloadData()
186
+
187
+ }
188
+
189
+
190
+
191
+
192
+
193
+ }
194
+
195
+
196
+
197
+
198
+
199
+ }
200
+
201
+
202
+
203
+ }
204
+
205
+
206
+
207
+
208
+
209
+
210
+
211
+ }
212
+
213
+ }
214
+
97
215
  ``````
98
-
99
-
100
-
101
-
102
-
103
- func loadData() {
104
-
105
- let db = Firestore.firestore()
106
-
107
- let shopsRef = db.collection("shops")
108
-
109
-
110
-
111
- shopsRef.getDocuments() { (snapshot, err) in
112
-
113
- if let err = err {
114
-
115
- print("Error getting documents: (err)")
116
-
117
- } else {
118
-
119
- if let snapshot = snapshot{
120
-
121
- for document in snapshot.documents {
122
-
123
-
124
-
125
- print(document.data())
126
-
127
- let data = document.data()
128
-
129
- let title = data["title"] as? String ?? ""
130
-
131
- let recomendation = data["recomendation"] as? String ?? ""
132
-
133
- let tel = data["tel"] as? String ?? ""
134
-
135
- let station = data["station"] as? String ?? ""
136
-
137
- let latitude = data["latitude"] as? Double ?? Double()
138
-
139
- let longitude = data["longitude"] as? Double ?? Double()
140
-
141
- let price = data["price"] as? Int ?? Int()
142
-
143
- let img = data["img"] as? String ?? ""
144
-
145
- let distance = data["distance"] as? CLLocationDistance ?? Double()
146
-
147
-
148
-
149
- let newShop = Shop(title: title, recomendation: recomendation, tel: tel, station: station, latitude: latitude, longitude: longitude, price: price, img: img, distance: distance)
150
-
151
- self.shopArray.append(newShop)
152
-
153
-
154
-
155
- func locationManager(_ manager: CLLocationManager, didUpdateLOcations locations: [CLLocation]){
156
-
157
- guard let currentLocation = manager.location else {
158
-
159
- print("location is nil")
160
-
161
- return
162
-
163
- }
164
-
165
- print("locations =(currentLocation.coordinate)")
166
-
167
-
168
-
169
-
170
-
171
- for i in self.shopArray.indices{
172
-
173
- // let currentLocation : CLLocation = CLLocation(latitude: localValue.latitude, longitude: localValue.longitude)
174
-
175
- let shopLocation : CLLocation = CLLocation(latitude: self.shopArray[i].latitude, longitude: self.shopArray[i].longitude)
176
-
177
- let distance = shopLocation.distance(from: currentLocation)
178
-
179
- self.shopArray[i].distance = distance //<-
180
-
181
-
182
-
183
- }
184
-
185
- self.shopArray.sort{$0.distance ?? 0.0 < $1.distance ?? 0.0}
186
-
187
- self.cafeTableView.reloadData()
188
-
189
- }
190
-
191
-
192
-
193
-
194
-
195
- }
196
-
197
-
198
-
199
-
200
-
201
- }
202
-
203
-
204
-
205
- }
206
-
207
-
208
-
209
-
210
-
211
-
212
-
213
- }
214
-
215
- }

3

様々なご回答を参考に修正をしたのですが、distanceがnilになってしまいます。shopArrayにデータを入れ込むコードを追記致しましたので、どの点に問題があるかをご指摘いただけると幸いです。

2018/09/01 09:51

投稿

rhcp56
rhcp56

スコア8

test CHANGED
File without changes
test CHANGED
@@ -82,32 +82,134 @@
82
82
 
83
83
  var img: String
84
84
 
85
+ var distance: CLLocationDistance?
86
+
87
+
88
+
85
89
 
86
90
 
87
- var dictionary: [String: Any] {
88
-
89
- return [
90
-
91
- "title": title,
92
-
93
- "recomendation": recomendation,
94
-
95
- "tel": tel,
96
-
97
- "station": station,
98
-
99
- "latitude": latitude,
100
-
101
- "longitude": longitude,
102
-
103
- "price": price,
104
-
105
- "img": img
106
-
107
- ]
91
+
92
+
93
+
94
+
95
+ }
96
+
97
+ ``````
98
+
99
+
100
+
101
+
102
+
103
+ func loadData() {
104
+
105
+ let db = Firestore.firestore()
106
+
107
+ let shopsRef = db.collection("shops")
108
+
109
+
110
+
111
+ shopsRef.getDocuments() { (snapshot, err) in
112
+
113
+ if let err = err {
114
+
115
+ print("Error getting documents: (err)")
116
+
117
+ } else {
118
+
119
+ if let snapshot = snapshot{
120
+
121
+ for document in snapshot.documents {
122
+
123
+
124
+
125
+ print(document.data())
126
+
127
+ let data = document.data()
128
+
129
+ let title = data["title"] as? String ?? ""
130
+
131
+ let recomendation = data["recomendation"] as? String ?? ""
132
+
133
+ let tel = data["tel"] as? String ?? ""
134
+
135
+ let station = data["station"] as? String ?? ""
136
+
137
+ let latitude = data["latitude"] as? Double ?? Double()
138
+
139
+ let longitude = data["longitude"] as? Double ?? Double()
140
+
141
+ let price = data["price"] as? Int ?? Int()
142
+
143
+ let img = data["img"] as? String ?? ""
144
+
145
+ let distance = data["distance"] as? CLLocationDistance ?? Double()
146
+
147
+
148
+
149
+ let newShop = Shop(title: title, recomendation: recomendation, tel: tel, station: station, latitude: latitude, longitude: longitude, price: price, img: img, distance: distance)
150
+
151
+ self.shopArray.append(newShop)
152
+
153
+
154
+
155
+ func locationManager(_ manager: CLLocationManager, didUpdateLOcations locations: [CLLocation]){
156
+
157
+ guard let currentLocation = manager.location else {
158
+
159
+ print("location is nil")
160
+
161
+ return
162
+
163
+ }
164
+
165
+ print("locations =(currentLocation.coordinate)")
166
+
167
+
168
+
169
+
170
+
171
+ for i in self.shopArray.indices{
172
+
173
+ // let currentLocation : CLLocation = CLLocation(latitude: localValue.latitude, longitude: localValue.longitude)
174
+
175
+ let shopLocation : CLLocation = CLLocation(latitude: self.shopArray[i].latitude, longitude: self.shopArray[i].longitude)
176
+
177
+ let distance = shopLocation.distance(from: currentLocation)
178
+
179
+ self.shopArray[i].distance = distance //<-
180
+
181
+
182
+
183
+ }
184
+
185
+ self.shopArray.sort{$0.distance ?? 0.0 < $1.distance ?? 0.0}
186
+
187
+ self.cafeTableView.reloadData()
188
+
189
+ }
190
+
191
+
192
+
193
+
194
+
195
+ }
196
+
197
+
198
+
199
+
200
+
201
+ }
202
+
203
+
204
+
205
+ }
206
+
207
+
208
+
209
+
210
+
211
+
212
+
213
+ }
108
214
 
109
215
  }
110
-
111
- }
112
-
113
- ``````

2

改善

2018/09/01 09:49

投稿

rhcp56
rhcp56

スコア8

test CHANGED
File without changes
test CHANGED
@@ -36,13 +36,13 @@
36
36
 
37
37
 
38
38
 
39
- Swift
40
-
41
39
  shopArrayにShop型のデータが突っ込んであります。
42
40
 
43
41
 
44
42
 
43
+ ```Swift
44
+
45
- func locationManager(_ manager: CLLocationManager, didUpdateLOcations locations: [CLLocation]){
45
+ func locationManager(_ manager: CLLocationManager, didUpdateLOcations locations: [CLLocation]){
46
46
 
47
47
  let localValue:CLLocationCoordinate2D = manager.location!.coordinate
48
48
 
@@ -110,4 +110,4 @@
110
110
 
111
111
  }
112
112
 
113
- ```
113
+ ``````

1

誤字

2018/08/28 04:46

投稿

rhcp56
rhcp56

スコア8

test CHANGED
File without changes
test CHANGED
@@ -36,7 +36,11 @@
36
36
 
37
37
 
38
38
 
39
+ Swift
40
+
39
- ```ここ言語名を入力
41
+ shopArrayShop型のデータが突っ込んであります。
42
+
43
+
40
44
 
41
45
  func locationManager(_ manager: CLLocationManager, didUpdateLOcations locations: [CLLocation]){
42
46
 
@@ -54,7 +58,7 @@
54
58
 
55
59
  let distance = shopLocation.distance(from: currentLocation)
56
60
 
57
- shopArray[i]["distance"] = "distance"
61
+ shopArray[i]["distance"] = "distance"#エラーはこの行に出ます。
58
62
 
59
63
 
60
64