如何使用iOS 7和8支持NSCalendar? [英] How to support NSCalendar with both iOS 7 and 8?

查看:294
本文介绍了如何使用iOS 7和8支持NSCalendar?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iOS 8为旧方法引入了一些新值。
例如,创建一个曾经是这样的日历:

iOS 8 introduced some new values for old methods. For instance, creating a calendar used to be like this:

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

现在,日历标识符已更改,我应该像这样创建对象:

Now, the calendar identifier has changed and I should create the object like so:

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

事情是,编译器仅在第一种情况下警告我不推荐使用NSGregorianCalendar。但是,编译器并没有警告我关于NSCalendarIdentifierGregorian与iOS 7的兼容性。

这是否意味着NSCalendarIdentifierGregorian在iOS 7下运行?

如果没有,根据操作系统创建带日历标识符的日历的最佳方法是什么?每次检查操作系统版本似乎很乏味。


谢谢。

Thing is, the compiler warns me only in the first situation that NSGregorianCalendar is deprecated. However, The compiler doesn't warn me at all about NSCalendarIdentifierGregorian compatibility with iOS 7.
Does it mean that NSCalendarIdentifierGregorian works under iOS 7 either?
If not, what's the best way of creating a calendar with a calendar identifier depending on the OS? checking OS version every single time seems tedious.

Thank you.

推荐答案

你可以使用内置的 __ IPHONE_8_0 宏,再加上自定义定义。

You can use the built-in __IPHONE_8_0 macro, coupled with custom defines.

您的自定义定义只会引用Objective-C中定义的基础值。由于这些是广泛使用的便利定义,我将它们粘贴到头文件中,并将其包含在我的PCH文件中(在Xcode 6中,您必须自己创建预编译器头,并在构建设置中指向它) 。

Your custom defines will simply refer to the underlying value defined in Objective-C. Since these are convenience defines that are widely used, I stick them into a header file, and include that in my PCH file (in Xcode 6, you have to create the pre-compiler header yourself, and point to it in your build settings).

当iOS 9发布时,您仍然可以使用 __ IPHONE_8_0 而无需更新,因为它仍然会为iOS 8之后的所有版本定义。

When iOS 9 is released, you will still be able to use __IPHONE_8_0 without updating, since it will still be defined, for all versions after iOS 8.

#ifdef __IPHONE_8_0
    #define GregorianCalendar NSCalendarIdentifierGregorian
#else
    #define GregorianCalendar NSGregorianCalendar
#endif



在您的实施文件中



In your implementation files

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:GregorianCalendar];

这篇关于如何使用iOS 7和8支持NSCalendar?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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