Home > Mobile >  Can't convert value of type [String] to expected argument type [Date] with dateFormatter
Can't convert value of type [String] to expected argument type [Date] with dateFormatter

Time:12-09

I am trying to get array of [Date] from [String], but I am getting an error: Cannot convert value of type '[String]' to expected argument type 'String'

I want to get three different dates to put them in different cells. There are three different dates in "list" property in my JSON

My Code:

//Array of Strings
let time = self.twentyFourHoursViewModel.twentyFourHoursWeather?.twentyFourHoursTime
                
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.locale = Locale(identifier: "ru_RU")
                
let dateDate = time.map({ dateFormatter.date(from: $0) }) <--Error here

My ViewModel:

class TwentyFourHoursViewModel {

// MARK: -Properties
let twentyFourHoursWeatherService: ITwentyFourHoursWeatherService

var twentyFourHoursWeather: TwentyFourHoursMainScreenWeatherModel?

init(twentyFourHoursWeatherService: ITwentyFourHoursWeatherService) {
    self.twentyFourHoursWeatherService = twentyFourHoursWeatherService
}

func twentyFourHoursViewDidLoad() {
    
    twentyFourHoursWeatherService.getCitiesWeather { [weak self] result in
        guard let self = self else { return }
        
        switch result {
        case .success(let result):
            self.twentyFourHoursWeather = .init(
                twentyFourHoursTime: [result.list[0].dtTxt, result.list[1].dtTxt, result.list[2].dtTxt],
                twentyFourHoursIcon: result.list[0].weather[0].icon,
                twentyFourHoursTemp: result.list[0].main.temp
            )
            self.twentyFourHoursWeatherDidChange?()
        case .failure(let error):
            print(error.localizedDescription)
        }
    }
}

var twentyFourHoursWeatherDidChange: (() -> Void)?

}

My Data:

struct TwentyFourHoursMainScreenWeatherModel {

let twentyFourHoursTime: [String]?
let twentyFourHoursIcon: String?
var twentyFourHoursTemp: Double? 

}

struct TwentyFourHoursCitiesWeather: Decodable {
    let cod: String
    let message, cnt: Int
    let list: [List]
    let city: City
}

struct List: Decodable {
    let dt: Int
    let main: TwentyFourHoursMain
    let weather: [TwentyFourHoursWeather]
    let clouds: TwentyFourHoursClouds
    let wind: TwentyFourHoursWind
    let visibility: Int
    let pop: Double
    let rain: Rain?
    let sys: TwentyFourHoursSys
    let dtTxt: String

My JSON:

{
"cod": "200",
"message": 0,
"cnt": 3,
"list": [
    {
        "dt": 1638986400,
        "main": {
            "temp": 279.95,
            "feels_like": 276.49,
            "temp_min": 278.97,
            "temp_max": 279.95,
            "pressure": 995,
            "sea_level": 995,
            "grnd_level": 994,
            "humidity": 68,
            "temp_kf": 0.98
        },
        "weather": [
            {
                "id": 803,
                "main": "Clouds",
                "description": "broken clouds",
                "icon": "04n"
            }
        ],
        "clouds": {
            "all": 75
        },
        "wind": {
            "speed": 5.58,
            "deg": 245,
            "gust": 12.34
        },
        "visibility": 10000,
        "pop": 0.1,
        "sys": {
            "pod": "n"
        },
        "dt_txt": "2021-12-08 18:00:00"
    },
    {
        "dt": 1638997200,
        "main": {
            "temp": 279.53,
            "feels_like": 275.54,
            "temp_min": 278.69,
            "temp_max": 279.53,
            "pressure": 996,
            "sea_level": 996,
            "grnd_level": 995,
            "humidity": 66,
            "temp_kf": 0.84
        },
        "weather": [
            {
                "id": 803,
                "main": "Clouds",
                "description": "broken clouds",
                "icon": "04n"
            }
        ],
        "clouds": {
            "all": 72
        },
        "wind": {
            "speed": 6.71,
            "deg": 246,
            "gust": 13.62
        },
        "visibility": 10000,
        "pop": 0,
        "sys": {
            "pod": "n"
        },
        "dt_txt": "2021-12-08 21:00:00"
    },
    {
        "dt": 1639008000,
        "main": {
            "temp": 278.98,
            "feels_like": 274.86,
            "temp_min": 278.5,
            "temp_max": 278.98,
            "pressure": 998,
            "sea_level": 998,
            "grnd_level": 996,
            "humidity": 71,
            "temp_kf": 0.48
        },
        "weather": [
            {
                "id": 803,
                "main": "Clouds",
                "description": "broken clouds",
                "icon": "04n"
            }
        ],
        "clouds": {
            "all": 80
        },
        "wind": {
            "speed": 6.62,
            "deg": 256,
            "gust": 12.17
        },
        "visibility": 10000,
        "pop": 0,
        "sys": {
            "pod": "n"
        },
        "dt_txt": "2021-12-09 00:00:00"
    }
],
"city": {
    "id": 2643743,
    "name": "London",
    "coord": {
        "lat": 51.5085,
        "lon": -0.1257
    },
    "country": "GB",
    "population": 1000000,
    "timezone": 0,
    "sunrise": 1638949983,
    "sunset": 1638978728
}

}

CodePudding user response:

You can try checking optional first, Or make twentyFourHoursTime object non optional

    //Array of Strings
    let time = self.twentyFourHoursViewModel.twentyFourHoursWeather?.twentyFourHoursTime
    guard let time = time else {
        //Handle error
        return
    }
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
    dateFormatter.locale = Locale(identifier: "ru_RU")
    
    let dateDate = time.map({ dateFormatter.date(from: $0) })

CodePudding user response:

It looks like twentyFourHoursTime is optional in TwentyFourHoursMainScreenWeatherModel struct.

struct TwentyFourHoursMainScreenWeatherModel 
{
let twentyFourHoursTime: [String]?
let twentyFourHoursIcon: String?
var twentyFourHoursTemp: Double? 
}

Try doing the following:

let dateDate = time?.map({ dateFormatter.date(from: $0) })

Calling out that time is optional.

In the below image - the last line is what you have in your code and the second-to-last line is my advice.

enter image description here

CodePudding user response:

check result which return in

getCitiesWeather

callback and make sure you deserialize your json properly as you expect

  • Related