user6510422 user6510422 - 3 months ago 16
Swift Question

Why does NSCalendar have two init methods with similar parameter types that accepts one of the calendar identifier constants?

Why does NSCalendar have two init methods with similar parameter types that both accept one of the calendar identifier constants? What is the point of having two init methods that do the exact same thing? Does anyone know the reasoning behind why Apple would add this information to the documentation this way or why the class is even created this way?

Here is the first init method:

init?(calendarIdentifier ident: String)


Here is the second init method:

init?(identifier calendarIdentifierConstant: String)


Link to Apple's NSCalendar Class Reference

Rob Rob
Answer

In Objective-C, there are two renditions, calendarWithIdentifier convenience method that returns autorelease object and initWithCalendarIdentifier that returns a +1 object. The distinction of the autorelease object and the +1 object has little practical implication nowadays (and really only comes into play if you were allocating and releasing many of them inside a loop, which you're unlikely to be doing with this class).

In Swift, they both resolve to init?, the first with a parameter called identifier and the other whose parameter is calendarIdentifier.

Note, in Swift 3, this confusion is eliminated, both converted to init?(identifier:).

Comments