NSDate() vs NSDate.date() in Swift

37,829

Solution 1

[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.

Solution 2

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

but now you should use NSDate()

Solution 3

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.

Solution 4

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.

Share:
37,829
max
Author by

max

Updated on July 09, 2022

Comments

  • max
    max almost 2 years

    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
    max over 9 years
    Actually NSDate.date() gives me a compiler error.
  • ChikabuZ
    ChikabuZ over 9 years
    Yes, old syntax no more supported
  • The Paramagnetic Croissant
    The Paramagnetic Croissant over 9 years
    @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.
  • max
    max over 9 years
    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
    max over 9 years
    Is there some setting in Xcode to get depreciation messages like this?
  • ChikabuZ
    ChikabuZ over 9 years
    I think that Apple decide to change syntax to more intuitive and short. [NSDate date] and NSDate() looks good, but NSDate.date() is worse.
  • The Paramagnetic Croissant
    The Paramagnetic Croissant over 9 years
    @papirtiger sorry, I don't get what you are referring to -- compiler errors are displayed in Xcode by default (as far as I know anyway... I don't use Xcode.)
  • max
    max over 9 years
    I only get 'date()' is unavailable: use object construction 'NSDate()' in Xcode. What compiler do you use?
  • The Paramagnetic Croissant
    The Paramagnetic Croissant over 9 years
    @papirtiger I use the very same Swift compiler that is bundled with Xcode (since there are no other compilers for Swift). I run it using xcrun swift.
  • radex
    radex over 9 years
    Perhaps you're not using the same version of Xcode. Apple changed this in 6.1.
  • Tommy
    Tommy over 8 years
    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.