*Updated to: Cannot value of ‘String’ to argument type ‘Date’ and have marked where I’m getting this in code,

I have and events in an file that I have parsed with XMLParser Delegate. I have a collectionview with a 30/31 day/month view and want to show white underlines for all the dates that have a matching date in my XML. I’m running into an Cannot convert value of type ‘String’ to expected argument type ‘Date’

Here’s my XML Parse:

//Mark: XML Parse Delegate

extension CalendarViewController {
func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) {
calendarEventsFromXML = elementName

func parser(_ parser: XMLParser, foundCharacters string: String) {
let data = string.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
if data.count != 0 {
    switch calendarEventsFromXML
    case "month": monthsFromXML = data
    case "date": datesFromXML = data
    case "datenumber": datenumbersFromXML = data
    case "holiday": holidaysFromXML = data
    default: break

func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
if elementName == "calendarevent"
    myCalendarDates.month = monthsFromXML = datesFromXML
    myCalendarDates.datenumber = datenumbersFromXML = holidaysFromXML

And my view controller:

class CalendarViewController: UIViewController, XMLParserDelegate {

let formatter: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.timeZone = Calendar.current.timeZone
dateFormatter.locale = Calendar.current.locale
dateFormatter.dateFormat = "yyyy MM dd"
return dateFormatter
} ()
let todaysDate = Date()

var tableViewDataSource = [CalendarDates]()
var myCalendarDates = CalendarDates()

override func viewDidLoad() {

//Mark: set event dates from XML == dates from Calendar to show a white eventDateUnderline 

func getEventDates(cell: CalendarCell, cellState: CellState) {
    for dates in calendarView.selectedDates {
        for calendarDate in tableViewDataSource {
            formatter.dateFormat = "yyyy MM dd"
            let datesFromCalendarXML = formatter.string(from:
            for dateOfEachEvent in datesFromCalendarXML{
                formatter.dateFormat = "yyyy MM dd"
                let dateInCalendar = formatter.string(from: dates)
                if dateOfEachEvent == dateInCalendar { //Error here: Cannot convert value of type 'String' to expected argument type 'Date'
                    cell.eventDateUnderline.isHidden = false
                } else {  
        print("Found (datesFromCalendarXML)") //Successfully prints all of the dates

class CalendarCell: JTAppleCell {
@IBOutlet weak var dateLabel: UILabel!
@IBOutlet weak var selectedView: UIView!
@IBOutlet weak var eventDateUnderline: UIImageView!

Can anyone help me understand why I am running into this error and how I can fix it? My thinking was that I can print all of the datesFromCalendarXML and so i should be able to write a boolean statement to match these against those in the calendar but I’m still missing something. I’m also converting the Date to a String with the dateFormatter, so I don’t know why its still comparing to Date.

Source link


Please enter your comment!
Please enter your name here