16

I've been following an Objective-C tutorial and the tutor is able to cast an NSTimeInterval object to a NSDate object.

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.

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?

Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
Halfpint
  • 3,967
  • 9
  • 50
  • 92

2 Answers2

30

There is a initializer of NSDate that takes NSTimeInterval:

init(timeIntervalSinceReferenceDate ti: NSTimeInterval)

So just do:

var date = NSDate(timeIntervalSinceReferenceDate: 123)
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
Peter Pei Guo
  • 7,770
  • 18
  • 35
  • 54
  • Thanks Peter, for some reason my XCode's intellisense isn't responding, didn't see those options when I first opened my constructor. – Halfpint Nov 24 '14 at 18:17
  • @Alex you don't *need* intellisense. :) – Lance Nov 24 '14 at 18:21
  • @Lance I'd probably know better, but I've only just started on iOS - need to aquatint myself with the docs ;) – Halfpint Nov 24 '14 at 18:28
  • 1
    Note that you are using the *designated* initializer `init(timeIntervalSinceReferenceDate ti: NSTimeInterval)`. This is correct, but does not match the first sentence of the answer. – Martin R Nov 24 '14 at 18:30
  • @PeterPeiGuo: That's not what I meant, I have (hopefully) fixed the answer. – Martin R Nov 24 '14 at 18:36
  • xcode intellisense not working?? wow you'd never think such a thing – Tyler Jun 10 '15 at 20:23
4

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.

The date of a post should most likely be stored as an NSDate object. Core Data handles NSDate objects just fine. 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". 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.

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

gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • Thanks gnasher. I too was confused why the author of the tutorial I followed didn't choose nsdate for both, he never even clarified his reasoning, but as it's my first time in swift and core data, I thought there would of maybe been a benefit, thank you for clearing my suspicions – Halfpint Nov 25 '14 at 12:42