创建新的NSCalendar日历 [英] Creating a new NSCalendar calendar

查看:192
本文介绍了创建新的NSCalendar日历的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在iPhone上使用NSCalendar在格里高利历和朱利安日历之间进行转换(注意:不是julian date)。如何将NSCalendar子类使用朱利安日历?

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.

推荐答案

虽然你肯定可以去做,但我怀疑 NSCalendar 是棘手的子类。它是免费的桥接,这使事情复杂化;与NSLocale交织在一起,这使事情变得复杂;它用标识符初始化而不是子类化,这使事情变得复杂;并且没有指导哪些方法是它的原语,...使...复杂化...

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...

你需要子类才能插入现有的架构吗?如果不是,从最初创建具有类似接口的对象可能是最容易的。您可以使用 NSGregorianCalendar 为您执行大部分工作,并在计算日期组件之前将日期偏移所需的天数。因为 NSGregorianCalendar 不会让你在一个闰日创建日期组件,你需要1700,1800和1900(当然2100等)的特殊逻辑。年,但这仍然比手工完成所有日期组件计算更容易。

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?

这篇关于创建新的NSCalendar日历的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆