Creating a new NSCalendar calendar

穿精又带淫゛_ 提交于 2019-12-11 01:35:23

问题


I want to be able to convert between the Gregorian and Julian calendars (note: not "julian date") using NSCalendar on the iPhone. How can I subclass NSCalendar to use the Julian calendar? I want to do this without regards for 1582 or 1752 or any given year.


回答1:


While you can certainly have a go at it, but I suspect that NSCalendar is tricky to subclass. It's toll-free bridged, which complicates things; is entwined with NSLocale, which complicates things; it's initialized with identifiers rather than being subclassed, which complicates things; and gives no guidance on which methods are its primitives which... complicates things...

Do you need the subclass in order to plug into an existing architecture? If not, it would probably be easiest to create an object from scratch that has a similar interface. You may be able to use an NSGregorianCalendar to do most of the work for you and just offset the date by the requisite days before calculating date components. You'll need special logic for 1700, 1800 and 1900 (and of course 2100, etc.) since NSGregorianCalendar won't let you create date components for a leap day in those years, but that's still probably easier than doing all the date component calculations by hand.

Just curious what the goal of such a calendar would be?



来源:https://stackoverflow.com/questions/1334318/creating-a-new-nscalendar-calendar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!