简体   繁体   中英

Missing argument for parameter 'delegate' in call // JSON file corrupted in SWIFT?

I am following an e-class tutorial for SWIFT // XCODE 11.4 and I have to fetch data from Open Weather API and display it on the interface where people can type in a city and the view controller will display temperature, cloud icon, and description.

Clima App Tutorial

I am using the MVC pattern design and the delegate design to accomplish this tutorial. My swift files are as followed:

Swift Files in MVC Design Pattern

Here are the codes in each of the important files:

I. Model folder

WeatherManager.swift

protocol WeatherManagerDelegate {
    func didUpdateWeather(weather: WeatherModel)
}

struct WeatherManager {
    let weatherURL = "https://api.openweathermap.org/d.5/weather?appid=c8b50079338280b47a65dd6082551e3b&units=imperial"

    let delegate: WeatherManagerDelegate?



    func fetchWeather(cityName: String) {
        let urlString = "\(weatherURL)&q=\(cityName)"
        performRequest(urlString: urlString)
    }

    func performRequest(urlString: String) {

        //create a URL
        if let url = URL(string: urlString) {

            //create a URLSession
            let session = URLSession(configuration: .default)

            //give session a task
            let task = session.dataTask(with: url) { (data, response, error) in
                if error != nil {
                    print(error!)
                    return //exit out of the func if there is an error
                }

                if let safeData = data {

                    if let weather =  self.parseJSON(weatherData: safeData) {
                    self.delegate?.didUpdateWeather(weather: weather)
                    }
                }
            }
            //start the tast
            task.resume()
        }
    }

    func parseJSON (weatherData: Data) -> WeatherModel? {
        let decoder = JSONDecoder()
        do {
            let decodedData = try decoder.decode(WeatherData.self, from: weatherData)
            let id = decodedData.weather[0].id
            let temp = decodedData.main.temp
            let name = decodedData.name


            let weather = WeatherModel(conditionId: id, cityName: name, temperature: temp)
            return weather

        } catch {
            print(error)
            return nil
        }
    }

}

WeatherData.swift

struct WeatherData: Codable {
    let name: String
    let main: Main
    let weather: [Weather]
}

struct Main: Codable {
    let temp: Double
}

struct Weather: Codable {
    let id: Int
}

WeatherModel.swift

struct WeatherModel {
    let conditionId: Int
    let cityName: String
    let temperature: Double

    var temperatureString: String {
        return String(format: "%.1f", temperature)
    }

    var conditionName: String {
        switch conditionId {
        case 200...232:
            return "cloud.bolt"
        case 300...321:
            return "cloud.drizzle"
        case 500...531:
            return "cloud.rain"
        case 600...622:
            return "cloud.snow"
        case 701...781:
            return "cloud.fog"
        case 800:
            return "sun.max"
        case 801...804:
            return "cloud.bolt"
        default:
            return "cloud"
        }
    }

}

II. Controller

WeatherViewController.swift (place where the error is)

class WeatherViewController: UIViewController, UITextFieldDelegate, WeatherManagerDelegate {

    @IBOutlet weak var conditionImageView: UIImageView!
    @IBOutlet weak var temperatureLabel: UILabel!
    @IBOutlet weak var cityLabel: UILabel!
    @IBOutlet weak var searchTextField: UITextField!

    var weatherManager = WeatherManager()

    override func viewDidLoad() {

        super.viewDidLoad()
        weatherManager.delegate = self

        searchTextField.delegate = self
    }

    @IBAction func searchPressed(_ sender: UIButton) {
        searchTextField.endEditing(true)
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        searchTextField.endEditing(true)
        print(searchTextField.text!)
        return true
    }

    func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
        if textField.text != "" {
            return true
        } else {
            textField.placeholder = "Type something..."
            return false
        }
    }

    func textFieldDidEndEditing(_ textField: UITextField) {
        if let city = searchTextField.text {
            weatherManager.fetchWeather(cityname: city)
        }

        searchTextField.text = ""

    }

    func didUpdateWeather(weather: WeatherModel) {
        print(weather.temperature)
    }


}

Here is the error message: Missing argument for parameter 'delegate' in call Error message in WeatherViewControl.swift

And when I hit the run button, I also got this error in the debug console:

dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.})))

Error in the debug console

What should I do to get rid of these errors?

You need to change the keyword "let" to "var" in let delegate: WeatherManagerDelegate? in struct WeatherManager .

Missing argument for parameter 'delegate' in call

When a struct is create a value for each property is required.

If each property is specified with a default value and there is no user-defined initializer then Swift will create a default initializer for the struct .

If there is at least one property without a default value and there is no user-defined initializer then Swift will create a memberwise initializer which has one parameter for each property without a default value.

For example your type:

struct WeatherModel {
   let conditionId: Int
   let cityName: String
   let temperature: Double
...

has three properties without default values. If you start typing:

let myWeatherModel = WeatherModel(

and then take the completion offered you will get (something like):

let wm = WeatherModel(conditionId: <Int>, cityName: <String>, temperature: <Double>)

The completion is showing the memberwise initializer .

Your type which produces the error is:

struct WeatherManager {
   let weatherURL = "https://api.openweathermap.org/d.5/weather?appid=c8b50079338280b47a65dd6082551e3b&units=imperial"
   let delegate: WeatherManagerDelegate?

which has two properties only one of which has a default value, and it has no initializers, so Swift will create a member wise initialiser automatically.

There is nothing wrong so far.

The line producing the error is:

var weatherManager = WeatherManager()

Here you are attempting to create a WeatherManager without invoking the member wise initalizer, so Swift gives you the error message.

If you click on the error message itself you will see a Fix is offered, click that and Swift will change your code to:

var weatherManager = WeatherManager(delegate: <WeatherManagerDelegate?>)

Select the <WeatherManagerDelegate?> and type the value you wish to pass.

HTH

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM