SwiftUIとXcodeで天気アプリを作っています。
自分の表示したい県庁所在地を入力すると入力した場所の天気情報が追加で表示される機能を実装中に以下のエラーメッセージが発生しました。
試行錯誤しましたがエラーが治りませんでした
詳しい方、頭のいい方、解決方法・訂正方法を教えていただけると嬉しいです。
発生している問題・エラーメッセージ
error1:Missing argument label 'contentsOf:' in call
場所:ContentViewModelのコメントアウト部
該当のソースコード
TabbarApp
1import SwiftUI 2 3@main 4struct TabbarApp: App { 5 var body: some Scene { 6 WindowGroup { 7 ContentView() 8 } 9 } 10}
ContentView
1import SwiftUI 2 3//ScheduleViewの作成 4割愛 5//WantToListViewの作成 6割愛 7//_____________________________________________________________________ 8//WeatherViewの作成 9struct ContentView: View{ 10 @StateObject var model = ContentViewModel() 11 12 var body: some View{ 13 NavigationView{ 14 TabView{ 15 AddCityView(model: model) 16 ForEach(model.cities){ city in 17 CityOverview(city: city) 18 } 19 } 20 .tabViewStyle(PageTabViewStyle(indexDisplayMode: .always)) 21 .navigationBarHidden(true) 22 .background( 23 Color.purple 24 .opacity(0.2)//色の濃さ 25 .ignoresSafeArea() 26 ) 27 } 28 } 29} 30//_____________________________________________________________________ 31//ChatListViewの作成 32割愛 33//Tab barの設定 34割愛 35struct ContentView_Previews: PreviewProvider { 36 static var previews: some View { 37 ContentView() 38 } 39}
ContentViewModel
1import Combine 2import SwiftUI 3 4final class ContentViewModel:ObservableObject{ 5 @Published var cities = [City]() 6 @Published var newCityName = "" 7 8 private var cancellables = Set<AnyCancellable>() 9 10 init() { 11 cities = City.placeholder 12 } 13 14 func addCity(){ 15 WeatherService 16 .getCity(for: newCityName) 17 .sink { (completion) in 18 switch completion{ 19 case .failure(let error): 20 print(error.localizedDescription) 21 return 22 case .finished: return 23 } 24 } receiveValue: { [weak self](city) in 25 DispatchQueue.main.async { 26###############error1################################### 27 self?.cities.append(city) 28######################################################## 29 } 30 } 31 .store(in: &cancellables) 32 33 } 34 35}
City
1import Foundation 2 3struct City:Codable, Identifiable{ 4 let id = UUID() 5 let name: String 6 let lat : Double 7 let lon: Double 8 9 static var placeholder:[City]{ 10 [ 11 City(name:"Tokyo",lat:0.0,lon:0.0), 12 City(name:"Osaka",lat:0.0,lon:0.0), 13 City(name:"Nagoya",lat:0.0,lon:0.0), 14 ] 15 16 } 17 18}
WeatherService
1import Combine 2import Foundation 3 4struct WeatherService{ 5 static func getWeatherData(lat:Double,long:Double,units:String = "metric") -> AnyPublisher<WeatherData,Error>{ 6 let url = URL(string: "https://api.openweathermap.org/data/2.5/onecall?lat=(lat)&lon=(long)&units=(units)&appid=(Statics.apiKey)")! 7 8 return URLSession.shared 9 .dataTaskPublisher(for: url) 10 .tryMap { element -> Data in 11 guard let httpResponse = element.response as? HTTPURLResponse, 12 httpResponse.statusCode == 200 else{ 13 throw URLError(.badServerResponse) 14 } 15 return element.data 16 } 17 .decode(type: WeatherData.self, decoder: JSONDecoder()) 18 .eraseToAnyPublisher() 19 } 20 21 static func getCity(for name:String) -> AnyPublisher<[City], Error>{ 22 let url = URL(string: "http://api.openweathermap.org/geo/1.0/direct?q=(name)&limit=1&appid=(Statics.apiKey)")! 23 24 return URLSession.shared 25 .dataTaskPublisher(for: url) 26 .tryMap { element -> Data in 27 guard let httpResponse = element.response as? HTTPURLResponse, 28 httpResponse.statusCode == 200 else{ 29 throw URLError(.badServerResponse) 30 } 31 return element.data 32 } 33 .decode(type: [City].self, decoder: JSONDecoder()) 34 .eraseToAnyPublisher() 35 } 36}
AddCityView
1import SwiftUI 2 3struct AddCityView: View { 4 @ObservedObject var model:ContentViewModel 5 var body: some View { 6 VStack{ 7 TextField("Name City",text:$model.newCityName ) 8 9 Button("Add City", action:model.addCity) 10 } 11 .padding() 12 .padding(.vertical, 8) 13 .frame(maxWidth: .infinity) 14 .background( 15 RoundedRectangle(cornerRadius: 40) 16 .fill(Color.white) 17 ) 18 .padding(.horizontal,25) 19 20 } 21}
WeatherData
1import Foundation 2 3struct WeatherData:Codable{ 4 struct Current:Codable{ 5 struct Weather:Codable{ 6 let id:Int 7 let main:String 8 let description:String 9 let icon:String 10 } 11 12 let dt:Date 13 let sunrise:Date 14 let sunset:Date 15 let temp:Double 16 let feelsLike:Double 17 let pressure:Int 18 let humidity:Int 19 let dewPoint:Double 20 let uvi:Double 21 let clouds:Int 22 let visibility:Int 23 let windSpeed:Double 24 let windDeg: Int 25 let windGust:Double 26 let weather:[Weather] 27 28 private enum CodingKeys:String,CodingKey{ 29 case dt 30 case sunrise 31 case sunset 32 case temp 33 case feelsLike = "feels_like" 34 case pressure 35 case humidity 36 case dewPoint = "dew_point" 37 case uvi 38 case clouds 39 case visibility 40 case windSpeed = "wind_speed" 41 case windDeg = "wind_deg" 42 case windGust = "wind_gust" 43 case weather 44 } 45 } 46 47 struct Minutely:Codable{ 48 let dt:Date 49 let precipitation:Int 50 } 51 52 struct Hourly:Codable{ 53 struct Weather:Codable{ 54 let id:Int 55 let main:String 56 let description:String 57 let icon:String 58 } 59 let dt:Date 60 let temp:Double 61 let feelsLike:Double 62 let pressure:Int 63 let humidity:Int 64 let dewPoint:Double 65 let uvi:Double 66 let clouds:Int 67 let visibility:Int 68 let windSpeed:Double 69 let windDeg: Int 70 let windGust:Double 71 let weather:[Weather] 72 let pop:Double 73 74 private enum CodingKeys:String,CodingKey{ 75 case dt 76 case temp 77 case feelsLike = "feels_like" 78 case pressure 79 case humidity 80 case dewPoint = "dew_point" 81 case uvi 82 case clouds 83 case visibility 84 case windSpeed = "wind_speed" 85 case windDeg = "wind_deg" 86 case windGust = "wind_gust" 87 case weather 88 case pop 89 } 90 } 91 92 struct Daily:Codable{ 93 struct Temp:Codable{ 94 let day:Double 95 let min:Double 96 let max:Double 97 let night:Double 98 let eve:Double 99 let morn:Double 100 } 101 struct FeelsLike:Codable{ 102 let day:Double 103 let night:Double 104 let eve:Double 105 let morn:Double 106 } 107 struct Weather:Codable{ 108 let id:Int 109 let main:String 110 let description:String 111 let icon:String 112 } 113 let dt:Date 114 let sunrise:Date 115 let sunset:Date 116 let temp:Temp 117 let feelsLike:FeelsLike 118 let pressure:Int 119 let humidity:Int 120 let dewPoint:Double 121 let windSpeed:Double 122 let windDeg: Int 123 let weather:[Weather] 124 let clouds:Int 125 let pop:Double 126 let uvi:Double 127 let rain:Double? 128 129 private enum CodingKeys:String,CodingKey{ 130 case dt 131 case sunrise 132 case sunset 133 case temp 134 case feelsLike = "feels_like" 135 case pressure 136 case humidity 137 case dewPoint = "dew_point" 138 case windSpeed = "wind_speed" 139 case windDeg = "wind_deg" 140 case weather 141 case clouds 142 case pop 143 case uvi 144 case rain 145 } 146 } 147 148 let lat :Double 149 let lon:Double 150 let timezone:String 151 let timezoneOffset:Int 152 let current:Current 153 let minutely:[Minutely] 154 let hourly:[Hourly] 155 let daily:[Daily] 156 157 private enum CodingKeys:String,CodingKey{ 158 case lat 159 case lon 160 case timezone 161 case timezoneOffset = "timezone_offset" 162 case current 163 case minutely 164 case hourly 165 case daily 166 } 167 168 169}
あなたの回答
tips
プレビュー