gpt4 book ai didi

swift - 在 Swift 中使用未解析的标识符 NSSecondCalendarUnit

转载 作者:搜寻专家 更新时间:2023-11-01 06:10:31 24 4
gpt4 key购买 nike

我尝试从当前日期获取小时分钟数:

var date:NSDate = NSDate()
var calendar:NSCalendar = NSCalendar.currentCalendar()
var components:NSDateComponents = calendar.components(unitFlags: NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit, fromDate: date)
var hour = components.hour
var minute = components.minute
var seconds = components.second

但是它不识别日历单位:

NSHourCalendarUnit, NSMinuteCalendarUnit andNSSecondCalendarUnit 

我得到错误:

use of unresolved identifier NSSecondCalendarUnit ...

我错过了什么吗?

最佳答案

改用 NSCalendarUnit 常量

var date:NSDate = NSDate()
var calendar:NSCalendar = NSCalendar.currentCalendar()
var components:NSDateComponents = calendar.components(
NSCalendarUnit.CalendarUnitHour | NSCalendarUnit.CalendarUnitMinute | NSCalendarUnit.CalendarUnitSecond, fromDate: date)
var hour = components.hour
var minute = components.minute
var seconds = components.second

引用:

来自 NSCalendar

func components(unitFlags: NSCalendarUnit, fromDate date: NSDate!) -> NSDateComponents!
^^^^^^^

关于swift - 在 Swift 中使用未解析的标识符 NSSecondCalendarUnit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25317418/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com