42

I´m following along with the Bloc.io Swiftris tutorial where they initialize a date by:

lastTick = NSDate.date()

Which causes a compile error:

'date()' is unavailable: use object construction 'NSDate()'

Which should equal:

NSDate *lastTick = [NSDate date];

(from the NSDate reference)

Did Apple change the Swift interface to NSDate, since I have seen other examples that use NSDate.date?

Is this just NSDate or can you not call type methods for any Objective-C APIs?

max
  • 96,212
  • 14
  • 104
  • 165
  • 5
    In Objective-C, `[NSDate date]` simply calls `[[NSDate alloc] init]`. Hence, you do not need to call `NSDate.date()` in Swift. Simply calling `NSDate()` will initialise a date object with the current date. – ZeMoon Oct 29 '14 at 11:04
  • @ZeMoon Why did you delete your answer? I was about to accept it. – max Oct 29 '14 at 11:11
  • Well, I thought it applied more as a comment, but apparently it was sufficient as the answer... Have undeleted it, you can vote it up at least, if not accept it... :D – ZeMoon Oct 29 '14 at 11:29
  • do you mistaken NSDate.date() instead of NSDate.init() ? – Sruit A.Suk Nov 22 '15 at 16:21
  • No, I was following a tutorial which used .date. It says so very clearly in the question. – max Nov 22 '15 at 16:22

4 Answers4

79

[NSDate date] is a factory method for constructing an NSDate object.

If you read the guide "Using Swift with Cocoa and Objective-C", there is a section on interacting with Objective-C apis:

For consistency and simplicity, Objective-C factory methods get mapped as convenience initializers in Swift. This mapping allows them to be used with the same concise, clear syntax as initializers.”

Excerpt From: Apple Inc. “Using Swift with Cocoa and Objective-C.” iBooks. https://itun.es/gb/1u3-0.l

So the factory method:

[NSDate date]

is converted into an initializer in Swift

NSDate()

It's not just NSDate where you will find this pattern, but in other Cocoa API's with factory methods.

Community
  • 1
  • 1
bandejapaisa
  • 26,576
  • 13
  • 94
  • 112
  • No doubt Apple wouldn't supply `[NSDate data]` in Objective-C either if the class were implemented from new now; ARC means that there's limited extra convenience in simple `alloc] init] autorelease];`-style factory methods. – Tommy Feb 03 '16 at 21:14
12

It seems that NSDate() is new syntax. Previously, NSDate.date() worked,

but now you should use NSDate()

swiftBoy
  • 35,607
  • 26
  • 136
  • 135
ChikabuZ
  • 10,031
  • 5
  • 63
  • 86
  • 1
    Actually `NSDate.date()` gives me a compiler error. – max Oct 29 '14 at 11:15
  • 1
    Yes, old syntax no more supported – ChikabuZ Oct 29 '14 at 11:16
  • @ChikabuZ your bad phrasing is misleading. If you meant that "previously, `NSDate.date()` worked", then don't write "earlier `NSDate.date()` works", because they mean completely different things. – The Paramagnetic Croissant Oct 29 '14 at 11:17
  • This does answer my question either, I already knew that I should use `NSDate()` from the error message in my question (I´m not an idiot). My question was about why the Swift interface is different from Obj.-C – max Oct 29 '14 at 11:20
  • 1
    I think that Apple decide to change syntax to more intuitive and short. [NSDate date] and NSDate() looks good, but NSDate.date() is worse. – ChikabuZ Oct 29 '14 at 11:25
8

In Objective-C, [NSDate date] simply calls [[NSDate alloc] init]. Hence, you do not need to call NSDate.date() in Swift. Simply calling NSDate() will initialise a date object with the current date.

ZeMoon
  • 20,054
  • 5
  • 57
  • 98
6

Did Apple change the Swift interface to NSDate?

Yes, they did. For me, the compiler tells:

Foundation.NSDate:3:26: note: 'date()' has been explicitly marked unavailable here:

@objc(date) class func date() -> Self!

So it is explicitly marked unavailable to Swift. This means that it has officially been deprecated.