質問編集履歴

3

情報の追加

2022/06/18 08:43

投稿

Adam_Yoneda
Adam_Yoneda

スコア10

test CHANGED
File without changes
test CHANGED
@@ -214,5 +214,22 @@
214
214
  }
215
215
 
216
216
  ```
217
-
217
+ ```swift
218
+ import Foundation
219
+
220
+ struct CoordinateModel {
221
+ let name: String
222
+ let lat: Double
223
+ let lon: Double
224
+
225
+ // wheather 地理座標を元に検索した天気の情報
226
+ let weatherstr = "https://api.openweathermap.org/data/2.5/weather?"
227
+ let str = "units=metric&appid=c8e60c6317c653a1789294c00f54ae19#"
228
+
229
+ // computedproperty
230
+ var urlString: String {
231
+ return "\(weatherstr)&lat=\(lat)&lon=\(lon)&\(str)"
232
+ }
233
+ }
234
+ ```
218
235
  よろしくお願いいたします。

2

情報不足

2022/06/18 08:38

投稿

Adam_Yoneda
Adam_Yoneda

スコア10

test CHANGED
File without changes
test CHANGED
@@ -134,5 +134,85 @@
134
134
  let temperature: Double
135
135
  }
136
136
  ```
137
+ ```swift
138
+
139
+ import Foundation
140
+
141
+ struct CoordinateManager {
142
+
143
+ // WeatherManager
144
+ let weathetManager = WeatherManager()
145
+ // geographical coordinates(lat, lon) 地理座標
146
+ let coordinateURL = "https://api.openweathermap.org/geo/1.0/direct?limit=1&appid=c8e60c6317c653a1789294c00f54ae19"
147
+
148
+ // fetchCoordinate
149
+ func fetchCoordinate(cityName: String) {
150
+ let urlString = "\(coordinateURL)&q=\(cityName)"
151
+ performRequest(inputURLString: urlString)
152
+ }
153
+
154
+ // transformURLString
155
+ func transformURLString(_ string: String) -> URLComponents? {
156
+ guard let urlPath = string.components(separatedBy: "?").first else {
157
+ return nil
158
+ }
159
+ var components = URLComponents(string: urlPath)
160
+ if let queryString = string.components(separatedBy: "?").last {
161
+ components?.queryItems = []
162
+ let queryItems = queryString.components(separatedBy: "&")
163
+ for queryItem in queryItems {
164
+ guard let itemName = queryItem.components(separatedBy: "=").first,
165
+ let itemValue = queryItem.components(separatedBy: "=").last else {
166
+ continue
167
+ }
168
+ components?.queryItems?.append(URLQueryItem(name: itemName, value: itemValue))
169
+ }
170
+ }
171
+ return components!
172
+ }
173
+
174
+ // performRequest
175
+ func performRequest(inputURLString: String) {
176
+ // 1. Create URL
177
+ let components = transformURLString(inputURLString)
178
+ if let url = components?.url {
179
+ // 2. Create URLSession
180
+ let session = URLSession(configuration: .default)
181
+ // 3. Give the URLSession a task
182
+ let task = session.dataTask(with: url) {(data, response, error) in
183
+ if error != nil {
184
+ print(error!)
185
+ return
186
+ }
187
+ if let safeData = data {
188
+ self.parseJSON(JSONobject: safeData)
189
+ }
190
+ }
191
+ // 4. Start the task
192
+ task.resume()
193
+ }
194
+ }
195
+
196
+ // parseJSON
197
+ func parseJSON(JSONobject: Data) {
198
+ let decoder = JSONDecoder()
199
+ do {
200
+ let decodedData = try decoder.decode([CoordinateData].self, from: JSONobject)
201
+ let name = decodedData[0].name
202
+ let lat = decodedData[0].lat
203
+ let lon = decodedData[0].lon
204
+
205
+ let coordinateModel = CoordinateModel(name: name, lat: lat, lon: lon)
206
+ let weatherURL = coordinateModel.urlString
207
+
208
+ // ------- WeatherManager fetchWeather ---------
209
+ weathetManager.fetchWeather(urlString: weatherURL)
210
+ } catch {
211
+ print(error)
212
+ }
213
+ }
214
+ }
215
+
216
+ ```
137
217
 
138
218
  よろしくお願いいたします。

1

内容の追加

2022/06/18 05:41

投稿

Adam_Yoneda
Adam_Yoneda

スコア10

test CHANGED
File without changes
test CHANGED
@@ -125,5 +125,14 @@
125
125
  }
126
126
  }
127
127
  ```
128
+ ```swift
129
+ import Foundation
130
+
131
+ struct WeatherModel {
132
+ let conditionId: Int
133
+ let cityName: String
134
+ let temperature: Double
135
+ }
136
+ ```
128
137
 
129
138
  よろしくお願いいたします。