Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 320 Vote(s) - 3.66 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NSDate() vs NSDate.date() in Swift

#1
I´m following along with the [Bloc.io Swiftris tutorial](

[To see links please register here]

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

[To see links please register here]

))


Did Apple change the [Swift interface to NSDate](

[To see links please register here]

), 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?
Reply

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

but **now you should use** **`NSDate()`**
Reply

#3
`[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.

[To see links please register here]


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

#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.
Reply

#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.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through