I am trying to extending NSDate
but am getting two errors :
extension NSDate { //'mutating' isn't valid on methods in classes or class-bound protocols
mutating func addMonth() {
let calendar = NSCalendar.currentCalendar()
let component = NSDateComponents()
component.month = 1
self = calendar.dateByAddingComponents(component, toDate: self, options: []) //Cannot assign to value: 'self' is immutable
}
}
My guess is that NSDate
is a class and not a Swift type and as the error states mutating
is not possible on methods in classes. If I return the value and assign it everything works but I want to know the exact reason this is not working and if there is a better workaround.