Reputation: 1943
I'm working on a project where I need to use a specific Calendar: Calendar.german
, no matter which are the user preferences.
I want to prevent future developers working on that project to use Calendar.current
.
Is there a way to override Calendar.current
to show a warning to point them to the right one (e.g. like Apple does with deprecation messages) ?
A different approach would be to override current
to return german
:
extension Calendar {
static var german: Calendar {
var calendar = Calendar(identifier: .gregorian)
calendar.locale = Locale(identifier: "de")
calendar.firstWeekday = 2
return calendar
}
static var current: Calendar {
return german
}
}
But I really wonder if it is possible the "warning" thing...
Upvotes: 3
Views: 147
Reputation: 271410
One way to do this would be to create your own current
property in a Calendar
extension, like you did here, and mark that as deprecated:
extension Calendar {
static var german: Calendar {
var calendar = Calendar(identifier: .gregorian)
calendar.locale = Locale(identifier: "de")
calendar.firstWeekday = 2
return calendar
}
@available(*, deprecated)
static var current: Calendar {
fatalError() // this implementation can be arbitrary
}
}
Note that this is actually making use of this bug, so it might stop working in the future.
Another way is to use the #warning("message")
syntax to issue warnings. But this will only issue the warning at the line where you're written it, but it could still serve as a way to tell future developers not to use Calendar.current
. Just put a
#warning("Don't use Calendar.current!")
somewhere and everyone would be able to see it in the error list.
Upvotes: 3
Reputation: 969
You can mark the method as deprecated and add some comment.
@available(*, deprecated, message: "use german")
static var current: Calendar {
return german
}
This shows warning 'current' is deprecated: use german
Upvotes: 6