In playgrounds this code works great and I get the expected output exactly as I want, but when I go put it in my Xcode project I get 3 warning with my startDate saying
Cannot convert value of type 'Date' to expected argument type 'Date'
Here is my code:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMMM, dd yyyy"
let date = dateFormatter.date(from: "June, 27 2018")
if let date = date {
let calendar = Calendar(identifier: .gregorian)
var startDate : Date = Date()
var interval : TimeInterval = 0
if calendar.dateInterval(of: .weekOfYear, start: &startDate, interval: &interval, for: date) {
print(startDate)
let daysToAdd = 6
var dateComponent = DateComponents()
dateComponent.day = daysToAdd
let futureDate = Calendar.current.date(byAdding: dateComponent, to: startDate)
let dateFormatter1 = DateFormatter()
dateFormatter1.dateFormat = "MMMM dd-"
let dateFormatter2 = DateFormatter()
let startWeek = dateFormatter1.string(from: startDate)
dateFormatter2.dateFormat = "dd"
let endWeek = dateFormatter2.string(from: futureDate!)
results = "\(startWeek)\(endWeek)"
}
}
The green text color on Date
type in
var startDate : Date = Date()
means that you have your own class/struct that is called Date
. That one cannot be converted to Foundation.Date
.
It's not a good idea to create this kind of name conflict in the first place but you can fix it just using the full name:
var startDate = Foundation.Date()