NSDate() vs NSDate.date() in Swift
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.
max
Updated on July 09, 2022Comments
-
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 over 9 yearsActually
NSDate.date()
gives me a compiler error. -
ChikabuZ over 9 yearsYes, old syntax no more supported
-
The Paramagnetic Croissant over 9 years@ChikabuZ your bad phrasing is misleading. If you meant that "previously,
NSDate.date()
worked", then don't write "earlierNSDate.date()
works", because they mean completely different things. -
max over 9 yearsThis 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 over 9 yearsIs there some setting in Xcode to get depreciation messages like this?
-
ChikabuZ over 9 yearsI 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 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 over 9 yearsI only get
'date()' is unavailable: use object construction 'NSDate()'
in Xcode. What compiler do you use? -
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 over 9 yearsPerhaps you're not using the same version of Xcode. Apple changed this in 6.1.
-
Tommy over 8 yearsNo 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 simplealloc] init] autorelease];
-style factory methods.