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

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

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

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

API

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

Q&A

1回答

915閲覧

SwiftとAPIに知見のある方、ご教授いただけませんか?

syabc124

総合スコア0

Swift

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

API

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

0グッド

0クリップ

投稿2021/08/17 10:05

SwiftとXcodeで天気アプリを作っています
変数はAPIより記入いたのですがエラーが出てしまいました。
詳しい方、解決方法・訂正方法を教えていただけると嬉しいです。
■■な機能を実装中に以下のエラーメッセージが発生しました。### 発生している問題・エラーメッセージ

  1. Value of type'[Daily Weather]'has no member'weather'
  2. Value of type'[Hourly Weather]'has no member'weather'
  3. Cannot assign value of type'[CurrentWeather]'to type 'CurrentWeather'

該当のソースコード

WeatherViewController

1import UIKit 2import CoreLocation 3 4// custom cell: collection view 5// API / request to get the data 6class WeatherViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, CLLocationManagerDelegate { 7 8 @IBOutlet var table: UITableView! 9 var models = [DailyWeatherEntry]() 10 var hourlyModels = [HourlyWeatherEntry]() 11 12 let locationManager = CLLocationManager() 13 var currentLocation: CLLocation? 14 var current: CurrentWeather? 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 23 table.delegate = self 24 table.dataSource = self 25 26 table.backgroundColor = UIColor(red: 52/255.0, green: 109/255.0, blue: 179/255.0, alpha: 1.0) 27 view.backgroundColor = UIColor(red: 52/255.0, green: 109/255.0, blue: 179/255.0, alpha: 1.0) 28 29 } 30 31 override func viewDidAppear(_ animated: Bool) { 32 super.viewDidAppear(animated) 33 setupLocation() 34 } 35 36 // Locationz 37 func setupLocation() { 38 locationManager.delegate = self 39 locationManager.requestWhenInUseAuthorization() 40 locationManager.startUpdatingLocation() 41 } 42 43 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 44 if !locations.isEmpty, currentLocation == nil { 45 currentLocation = locations.first 46 locationManager.stopUpdatingLocation() 47 requestWeatherForLocation() 48 } 49 } 50 51 func requestWeatherForLocation() { 52 guard let currentLocation = currentLocation else { 53 return 54 } 55 let long = currentLocation.coordinate.longitude 56 let lat = currentLocation.coordinate.latitude 57 58 let url = "https://api.openweathermap.org/data/2.5/onecall?lat=(lat)&lon=(long)&exclude=flags,minutely&appid=2855f534d93973336e3e932f039b7258" 59 60 URLSession.shared.dataTask(with: URL(string: url)!, completionHandler: { data, response, error in 61 62 // Validation 63 guard let data = data, error == nil else { 64 print("something went wrong") 65 return 66 } 67 68 // Convert data to models/some object 69 var json: WeatherResponse? 70 do { 71 json = try JSONDecoder().decode(WeatherResponse.self, from: data) 72 } 73 catch { 74 print("error: (error)") 75 } 76 77 guard let result = json else { 78 return 79 } 80 81 let entries = result.daily.weather//エラー箇所 82 self.models.append(entries)//エラー箇所 83 84 let current = result.current//エラー箇所 85       self.current = current//エラー箇所 86 87 self.hourlyModels = result.hourly.weather//エラー箇所 88 89 // Update user interface 90 DispatchQueue.main.async { 91 self.table.reloadData() 92 93 self.table.tableHeaderView = self.createTableHeader() 94 } 95 96 }).resume() 97 } 98 99 func createTableHeader() -> UIView { 100 let headerVIew = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.width)) 101 102 headerVIew.backgroundColor = UIColor(red: 52/255.0, green: 109/255.0, blue: 179/255.0, alpha: 1.0) 103 104 let locationLabel = UILabel(frame: CGRect(x: 10, y: 10, width: view.frame.size.width-20, height: headerVIew.frame.size.height/5)) 105 let summaryLabel = UILabel(frame: CGRect(x: 10, y: 20+locationLabel.frame.size.height, width: view.frame.size.width-20, height: headerVIew.frame.size.height/5)) 106 let tempLabel = UILabel(frame: CGRect(x: 10, y: 20+locationLabel.frame.size.height+summaryLabel.frame.size.height, width: view.frame.size.width-20, height: headerVIew.frame.size.height/2)) 107 108 headerVIew.addSubview(locationLabel) 109 headerVIew.addSubview(tempLabel) 110 headerVIew.addSubview(summaryLabel) 111 112 tempLabel.textAlignment = .center 113 locationLabel.textAlignment = .center 114 summaryLabel.textAlignment = .center 115 116 locationLabel.text = "Current Location" 117 118 guard let currentWeather = self.current else { 119 return UIView() 120 } 121 122 tempLabel.text = "(currentWeather.temp)°" 123 tempLabel.font = UIFont(name: "Helvetica-Bold", size: 32) 124 summaryLabel.text = self.current?.weather.description 125 126 return headerVIew 127 } 128 129 130 // Table 131 func numberOfSections(in tableView: UITableView) -> Int { 132 return 2 133 } 134 135 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 136 if section == 0 { 137 // 1 cell that is collectiontableviewcell 138 return 1 139 } 140 // return models count 141 return models.count 142 } 143 144 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 145 if indexPath.section == 0 { 146 let cell = tableView.dequeueReusableCell(withIdentifier: HourlyTableViewCell.identifier, for: indexPath) as! HourlyTableViewCell 147 cell.configure(with: hourlyModels) 148 cell.backgroundColor = UIColor(red: 52/255.0, green: 109/255.0, blue: 179/255.0, alpha: 1.0) 149 return cell 150 } 151 152 // Continue 153 let cell = tableView.dequeueReusableCell(withIdentifier: WeatherTableViewCell.identifier, for: indexPath) as! WeatherTableViewCell 154 cell.configure(with: models[indexPath.row]) 155 cell.backgroundColor = UIColor(red: 52/255.0, green: 109/255.0, blue: 179/255.0, alpha: 1.0) 156 return cell 157 } 158 159 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 160 return 100 161 } 162 163} 164 165struct WeatherResponse: Codable { 166 let lat: Float 167 let lon: Float 168 let timezone: String 169 let timezone_offset: Float 170 let current: [CurrentWeather] 171 let hourly: [HourlyWeather] 172 let daily: [DailyWeather] 173} 174 175struct CurrentWeather: Codable { 176 let dt: Int 177 let sunrise: Int 178 let sunset: Int 179 let temp: Double 180 let feel_like: Double 181 let pressure: Double 182 let humidity: Double 183 let dew_Point: Double 184 let uvi: Int 185 let clouds: Double 186 let visibility: Double 187 let wind_speed: Double 188 let wind_deg: Double 189 let weather :[CurrentWeatherEntry] 190 let minutely: [CurrentMinutelyEntry] 191} 192 193struct CurrentWeatherEntry: Codable { 194 let id : Double 195 let main:String 196 let description:String 197 let icon: String 198} 199 200struct CurrentMinutelyEntry: Codable { 201 let dt : Int 202 let presipitation:Double 203} 204 205struct DailyWeather: Codable { 206 let dt: Int 207 let sunrise: Int 208 let sunset: Int 209 let moonrise:Int 210 let moonset: Int 211 let moon_phase:Double 212 let temp: [DailyTempEntry] 213 let feel_like: [DailyFeelEntry] 214 let pressure: Double 215 let humidity: Double 216 let dew_Point: Double 217 let uvi: Int 218 let clouds: Double 219 let visibility: Double 220 let wind_speed: Double 221 let wind_deg: Double 222 let weather :[DailyWeatherEntry] 223 let pop: Double 224 let rain: Double 225} 226 227struct DailyTempEntry: Codable { 228 let day: Double 229 let min: Double 230 let max: Double 231 let night: Double 232 let eve: Double 233 let morn: Double 234} 235 236struct DailyFeelEntry: Codable { 237 let day: Double 238 let night: Double 239 let eve: Double 240 let morn: Double 241} 242 243struct DailyWeatherEntry: Codable { 244 let id : Double 245 let main:String 246 let description:String 247 let icon: String 248} 249struct HourlyWeather: Codable { 250 let dt: Int 251 let sunrise: Int 252 let sunset: Int 253 let temp: Double 254 let feel_like: Double 255 let pressure: Double 256 let humidity: Double 257 let dew_Point: Double 258 let uvi: Int 259 let clouds: Double 260 let visibility: Double 261 let wind_speed: Double 262 let wind_deg: Double 263 let weather :[HourlyWeatherEntry] 264} 265 266struct HourlyWeatherEntry: Codable { 267 let id : Double 268 let main:String 269 let description:String 270 let icon: String 271}

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

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

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

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

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

guest

回答1

0

エラーメッセージを読みましょう。

Value of type '[Daily Weather]' has no member 'weather'

配列にアクセスしようとしていますが、配列には weather というメンバーはありません。

Value of type '[Hourly Weather]' has no member 'weather'

同じく配列にアクセスしようとしていますが、配列には weather というメンバーはありません。

Cannot assign value of type '[CurrentWeather]' to type 'CurrentWeather'

配列を配列でない値に代入しようとしています。

投稿2021/08/17 10:20

mather

総合スコア6753

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

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

syabc124

2021/08/17 14:01

初心者に丁寧に教えていただきありがとうございます!!! たすかりました!!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

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

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

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問