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

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

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

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

Swift

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

API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

Q&A

0回答

718閲覧

typeMismatcのエラーを無くすためには?

syabc124

総合スコア0

Xcode

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

Swift

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

API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

0グッド

0クリップ

投稿2021/08/17 13:57

SwiftとXcodeで天気アプリを作っています
タイプエラーが出てしまいました。
試行錯誤しましたがエラーが治りませんでした
詳しい方、解決方法・訂正方法を教えていただけると嬉しいです。
■■な機能を実装中に以下のエラーメッセージが発生しました。### 発生している問題・エラーメッセージ
error: typeMismatch(Swift.Dictionary<Swift.String, Any>, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "current", intValue: nil), CodingKeys(stringValue: "weather", intValue: nil)], debugDescription: "Expected to decode Dictionary<String, Any> but found an array instead.", underlyingError: nil))

該当のソースコード

WetherViewController

1import UIKit 2import CoreLocation 3 4// custom cell: collection view 5// API / request to get the data 6class WetherViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,CLLocationManagerDelegate{ 7 8 @IBOutlet var table: UITableView! 9 10 var models = [WeatherResponse]() 11 12 let locationManager = CLLocationManager() 13 14 var currentLocation: CLLocation? 15 16 override func viewDidLoad() { 17 super.viewDidLoad() 18 19 // Register 2 cells 20 table.register(HourlyTableViewCell.nib(), forCellReuseIdentifier: HourlyTableViewCell.identifier) 21 table.register(WeatherTableViewCell.nib(), forCellReuseIdentifier: WeatherTableViewCell.identifier) 22 table.delegate = self 23 table.dataSource = self 24 } 25 26 override func viewDidAppear(_ animated: Bool) { 27 super.viewDidAppear(animated) 28 setupLocation() 29 30 } 31 32 //Location 33 func setupLocation() { 34 locationManager.delegate = self 35 locationManager.requestWhenInUseAuthorization() 36 locationManager.startUpdatingLocation() 37 38 } 39 40 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 41 if !locations.isEmpty, currentLocation == nil { 42 currentLocation = locations.first 43 locationManager.stopUpdatingLocation() 44 requestWeatherForLocation() 45 46 } 47 48 } 49 func requestWeatherForLocation() { 50 guard let currentLocation = currentLocation else { 51 return 52 53 } 54 let long = currentLocation.coordinate.longitude 55 let lati = currentLocation.coordinate.latitude 56 57 let url = "https://api.openweathermap.org/data/2.5/onecall?lat=(lati)&lon=(long)&exclude=flags,minutely&appid=2855f534d93973336e3e932f039b7258" 58 print(url) 59 URLSession.shared.dataTask(with: URL(string: url)!, completionHandler: { data, response, error in 60 // Validation 61 guard let data = data, error == nil else { 62 print("何か問題が発生しています") 63 return 64 65 } 66 67 // Convert data to models/some object 68 var json: WeatherResponse? 69 do { 70 json = try JSONDecoder().decode(WeatherResponse.self, from: data) 71 72 } 73 catch { 74 print("error: (error)") 75 76 } 77 guard let result = json else { 78 return 79 80 } 81 print(result.current.weather.main) 82 83 84 // Update user interface 85 86 }).resume() 87 88 } 89 90 //Table 91 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 92 return models.count 93 } 94 95 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 96 return UITableViewCell() 97 } 98 99} 100 101struct WeatherResponse: Codable { 102 let lat: Float 103 let lon: Float 104 let timezone: String 105 let timezone_offset: Float 106 let current: CurrentWeather 107 let hourly: HourlyWeather 108 let daily: DailyWeather 109} 110 111struct CurrentWeather: Codable { 112 let dt: Int 113 let sunrise: Int 114 let sunset: Int 115 let temp: Double 116 let feels_like: Double 117 let pressure: Double 118 let humidity: Double 119 let dew_point: Double 120 let uvi: Double 121 let clouds: Double 122 let visibility: Double 123 let wind_speed: Double 124 let wind_deg: Double 125 let weather :CurrentWeatherEntry 126 let minutely: CurrentMinutelyEntry 127} 128 129struct CurrentWeatherEntry: Codable { 130 let id : Int 131 let main:String 132 let description:String 133 let icon: String 134} 135 136struct CurrentMinutelyEntry: Codable { 137 let dt : Int 138 let presipitation:Double 139} 140 141struct DailyWeather: Codable { 142 let dt: Int 143 let sunrise: Int 144 let sunset: Int 145 let moonrise:Int 146 let moonset: Int 147 let moon_phase:Double 148 let temp: DailyTempEntry 149 let feels_like: DailyFeelEntry 150 let pressure: Double 151 let humidity: Double 152 let dew_point: Double 153 let uvi: Double 154 let clouds: Double 155 let visibility: Double 156 let wind_speed: Double 157 let wind_deg: Double 158 let weather :DailyWeatherEntry 159 let pop: Double 160 let rain: Double 161} 162 163struct DailyTempEntry: Codable { 164 let day: Double 165 let min: Double 166 let max: Double 167 let night: Double 168 let eve: Double 169 let morn: Double 170} 171 172struct DailyFeelEntry: Codable { 173 let day: Double 174 let night: Double 175 let eve: Double 176 let morn: Double 177} 178 179struct DailyWeatherEntry: Codable { 180 let id : Int 181 let main:String 182 let description:String 183 let icon: String 184} 185 186struct HourlyWeather: Codable { 187 let dt: Int 188 let sunrise: Int 189 let sunset: Int 190 let temp: Double 191 let feels_like: Double 192 let pressure: Double 193 let humidity: Double 194 let dew_point: Double 195 let uvi: Double 196 let clouds: Double 197 let visibility: Double 198 let wind_speed: Double 199 let wind_deg: Double 200 let weather :HourlyWeatherEntry 201} 202 203struct HourlyWeatherEntry: Codable { 204 let id : Int 205 let main:String 206 let description:String 207 let icon: String 208}

API

1{ 2 "lat": 33.44, 3 "lon": -94.04, 4 "timezone": "America/Chicago", 5 "timezone_offset": -21600, 6 "current": { 7 "dt": 1618317040, 8 "sunrise": 1618282134, 9 "sunset": 1618333901, 10 "temp": 284.07, 11 "feels_like": 282.84, 12 "pressure": 1019, 13 "humidity": 62, 14 "dew_point": 277.08, 15 "uvi": 0.89, 16 "clouds": 0, 17 "visibility": 10000, 18 "wind_speed": 6, 19 "wind_deg": 300, 20 "weather": [ 21 { 22 "id": 500, 23 "main": "Rain", 24 "description": "light rain", 25 "icon": "10d" 26 } 27 ], 28 "rain": { 29 "1h": 0.21 30 } 31 }, 32 "minutely": [ 33 { 34 "dt": 1618317060, 35 "precipitation": 0.205 36 }, 37 ... 38 }, 39 "hourly": [ 40 { 41 "dt": 1618315200, 42 "temp": 282.58, 43 "feels_like": 280.4, 44 "pressure": 1019, 45 "humidity": 68, 46 "dew_point": 276.98, 47 "uvi": 1.4, 48 "clouds": 19, 49 "visibility": 306, 50 "wind_speed": 4.12, 51 "wind_deg": 296, 52 "wind_gust": 7.33, 53 "weather": [ 54 { 55 "id": 801, 56 "main": "Clouds", 57 "description": "few clouds", 58 "icon": "02d" 59 } 60 ], 61 "pop": 0 62 }, 63 ... 64 } 65 "daily": [ 66 { 67 "dt": 1618308000, 68 "sunrise": 1618282134, 69 "sunset": 1618333901, 70 "moonrise": 1618284960, 71 "moonset": 1618339740, 72 "moon_phase": 0.04, 73 "temp": { 74 "day": 279.79, 75 "min": 275.09, 76 "max": 284.07, 77 "night": 275.09, 78 "eve": 279.21, 79 "morn": 278.49 80 }, 81 "feels_like": { 82 "day": 277.59, 83 "night": 276.27, 84 "eve": 276.49, 85 "morn": 276.27 86 }, 87 "pressure": 1020, 88 "humidity": 81, 89 "dew_point": 276.77, 90 "wind_speed": 3.06, 91 "wind_deg": 294, 92 "weather": [ 93 { 94 "id": 500, 95 "main": "Rain", 96 "description": "light rain", 97 "icon": "10d" 98 } 99 ], 100 "clouds": 56, 101 "pop": 0.2, 102 "rain": 0.62, 103 "uvi": 1.93

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

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

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

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

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

hoshi-takanori

2021/08/17 17:31

API の結果を見る限り weather は配列なので、配列として受け取った上で処理する必要があります。 (というか、前回の質問が解決してなかったということでは…。)
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問