Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - Xcode complaining about extra argument - NSDate initialisation

Tags:

date

ios

swift

I'm simply trying to initialise an NSDate object from epoch. I have a dictionary object of type Dictionary<String, AnyObject> and I know that this key corresponds to an Int

But the Swift compiler is complaining that the NSDate line has an Extra argument 'timeIntervalSince1970' in call

 if let respondedDate : Int = (responseDict["expiry_date"] as AnyObject) as? Int {
     let expiryDate = NSDate(timeIntervalSince1970: respondedDate)
 }

No idea what I am doing wrong here, this seems completely correct to me. Any ideas?

Thanks for the timely response. Here's the working code!

if let respondedDate : NSTimeInterval = (responseDict["expiry_date"] as AnyObject) as? NSTimeInterval {
    let expiryDate = NSDate(timeIntervalSince1970: respondedDate)
}

Hopefully Xcode will be updated soon to make this error more descriptive rather than telling me that the there's an "extra argument"

like image 336
bzmw Avatar asked Aug 20 '14 15:08

bzmw


2 Answers

It's expecting respondedDate to be a NSTimeInterval which is a Double. If you cast respondedDate to NSTimeInterval instead it should work.

like image 114
Sherman Lo Avatar answered Oct 15 '22 07:10

Sherman Lo


Declare respondedDate as an NSTimeInterval instead of an Int. Swift is strongly typed, so it isn't able to automatically cast an Int to NSTimeInterval (which is a typealias for Double).

like image 45
Kamaros Avatar answered Oct 15 '22 09:10

Kamaros