I implemented a function to get the start date of the week as the following.
import UIKit
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let date = dateFormatter.date(from: "2017-01-07")
let calender = NSCalendar(identifier:NSCalendar.Identifier.gregorian)!
let flags :NSCalendar.Unit = [NSCalendar.Unit.year, NSCalendar.Unit.month, NSCalendar.Unit.weekOfYear, NSCalendar.Unit.weekday]
var components = calender.components(flags, from: date!)
components.weekday = 1 // Sunday
components.hour = 0
components.minute = 0
components.second = 0
print(calender.date(from: components)!)
When the date is '2017-05-07', the start date of the week is right.
But when the date is '2017-01-07', the result is '2017-12-30'.
Who know the reason and how to fix.
Thanks