简体   繁体   English

SWIFT将NSTimeInteval转换为NSDate

[英]SWIFT convert NSTimeInteval to NSDate

I've been following an Objective-C tutorial and the tutor is able to cast an NSTimeInterval object to a NSDate object. 我一直在关注Objective-C教程,导师能够将NSTimeInterval对象强制转换为NSDate对象。

The lesson uses CoreData, and stores the date of a post as an NSTimeInterval, later on we want to retrieve that interval and set it as a formatted date string to present as a section title in a UITableVIewController. 本课程使用CoreData,并将帖子的日期存储为NSTimeInterval,稍后我们要检索该间隔并将其设置为格式化的日期字符串,以在UITableVIewController中显示为节标题。

class DiaryEntry: NSManagedObject {

@NSManaged var date: NSTimeInterval
@NSManaged var body: String
@NSManaged var imageData: NSData
@NSManaged var mood: Int16
@NSManaged var location: String

func sectionName() -> String {
    let date = NSDate().dateByAddingTimeInterval(self.date)
    let f = NSDateFormatter()
    f.dateFormat = "MMM yyy"

    return f.stringFromDate(date)
}

}

Mainly concerned with the line: 主要关注的是:

let date:NSDate = NSDate().dateByAddingTimeInterval(self.date)

Which right now is actually adding the set date onto the current date, and this is not the behaviour I want. 现在哪个实际上是将设置日期添加到当前日期,这不是我想要的行为。

How do I cast the self.date variable to an NSDate object in SWIFT? 如何将self.date变量NSDate为SWIFT中的NSDate对象?

There is a initializer of NSDate that takes NSTimeInterval : 有一个NSDateinitializer ,它采用NSTimeInterval

init(timeIntervalSinceReferenceDate ti: NSTimeInterval)

So just do: 所以这样做:

var date = NSDate(timeIntervalSinceReferenceDate: 123)

This code is really confused. 这段代码真的很混乱。

A time interval is not a date. 时间间隔不是日期。 A date is a point in time. 日期是一个时间点。 A time interval is the difference in seconds between two points in time. 时间间隔是两个时间点之间的秒数差。 Whenever you have a time interval, you have the question "it is the number of seconds between what two dates? You correctly realise that adding a time interval stored in a database to NSDate () is unlikely to give a useful result, because the same call executed 10 seconds later will give a different date. 每当你有一个时间间隔时,你会有一个问题“它是两个日期之间的秒数?你正确地意识到将数据库中存储的时间间隔添加到NSDate()不太可能产生有用的结果,因为同样的10秒后执行的呼叫将给出不同的日期。

The date of a post should most likely be stored as an NSDate object. 帖子的日期应该最有可能存储为NSDate对象。 Core Data handles NSDate objects just fine. Core Data处理NSDate对象就好了。 If you want to store time intervals, the date of the post must be converted to a time interval since some fixed reference date; 如果要存储时间间隔,则必须将帖子的日期转换为自某个固定参考日期以来的时间间隔; you do that for example using "timeIntervalSinceReferenceDate". 你这样做是为了使用“timeIntervalSinceReferenceDate”。 If you do that, I very strongly recommend that you don't call the variable "date" but something like "secondsSinceReferenceDate" which makes it obvious what to store when you are given a date, and how to convert that number back to an NSDate. 如果你这样做,我强烈建议你不要调用变量“date”,而是调用“secondsSinceReferenceDate”,这样可以明确给出日期时要存储的内容,以及如何将该数字转换回NSDate 。

(The reason to call it "secondsSinceReferenceDate" is because there is plenty of code that tries to store milliseconds, or nanoseconds, and there is plenty of code that stores intervals since the epoch (Jan 1st 1970), so it's really good if someone reading your code knows immediately what the numbers mean by just looking at the variable name). (将其称为“secondsSinceReferenceDate”的原因是因为有大量代码试图存储毫秒或纳秒,并且自从纪元(1970年1月1日)以来存在大量存储间隔的代码,所以如果有人读取它真的很好你的代码只需查看变量名即可立即知道数字的含义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM