ios - 我如何解决 ios8 的日期问题

标签 ios swift ios8 nscalendar gregorian-calendar

我不知道如何编写正确的代码来解决我的日期问题

class Date {

class func from(#year:Int, month:Int, day:Int) -> NSDate{

    var components = NSDateComponents()
    components.year = year
    components.month = month
    components.day = day

  let gregorianCalendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)
    let date = gregorianCalendar?.dateFromComponents(components)

    return date!
}

这是错误信息 “NSGregorianCalendar”在 iOS 版本 8.0 中已弃用:使用 NSCalendarIdentifierGregorian 代替

但是当我替换下一行时不起作用

最佳答案

只需使用:

let calendar = NSCalendar.currentCalendar()

它将返回您的公历(取决于区域设置)。

另一个选项是定义常量并使用 GregorianCalendar 作为日历名称:

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

关于ios - 我如何解决 ios8 的日期问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31639161/

相关文章:

iphone - 通用 UITableView 键盘大小调整算法

ios - 如何更改 NSMutableDictionary 中现有对象的键?

ios - 未调用调度组功能 - 如何使用调度组?

ios - 在 SwiftUI Form 中填充 Section 背景

swift - UIStoryboard 中的 iOS 8 自定义选项卡栏 Controller

ios - 我将部署目标设置为 iOS8 ,应用商店显示 "Compatibility: Requires iOS 7.0 or later"。我可以强制应用程序仅安装在 iOS8 中吗?

ios - iOS 如何在 NSURLRequests 中设置 X-Forwarded-For header ?

iOS - 顶部布局指南高度问题

ios - AppDelegate 中的 Firebase addStateDidChangeListener

ios - UIAlertController - 更改文本字段的大小并在它们之间添加空格