1

I have this method in Objective-C to return the time stamp of the location sent to the server.

-(NSDate *) toLocalTime { NSTimeZone *tz = [NSTimeZone defaultTimeZone]; NSInteger seconds = [tz secondsFromGMTForDate: [NSDate date]]; return [NSDate dateWithTimeInterval: seconds sinceDate: [NSDate date]]; } 

This works perfectly in Obj-C I have converted it to swift 2.0

func toLocalTime() -> NSDate { var tz : NSTimeZone = NSTimeZone.defaultTimeZone() var seconds: Int = tz.secondsFromGMTForDate(NSDate()) return (NSDate(timeInterval: seconds, sinceDate: NSDate())) } 

but when I convert this to swift 2.0 , error(mentioned as the title above) is shown for this line as the variable seconds is of type Int and the variable that should be passed to dateWithTimeInterval should be NSTimeInterval.

Error:Cannot convert the type 'int' to expected type NSTimeInterval

Error line: return (NSDate(timeInterval: seconds, sinceDate: NSDate()))

please help !

1
  • can you not just go var seconds: NSTimeInterval = tz.secondsFromGMTForDate(NSDate()) ? Commented Mar 16, 2016 at 6:03

3 Answers 3

5

If you check Date class .. you can found public typealias NSTimeInterval = Double means return type is Double for NSTimeInterval ..

and the method is

public convenience init(timeInterval secsToBeAdded: NSTimeInterval, sinceDate date: NSDate) // timeInterval -> return NSTimeInterval // date -> return NSDate 

timeInterval takes Double not int ... so just convert it to Double like

 func toLocalTime() -> NSDate { let tz = NSTimeZone.defaultTimeZone() let seconds = tz.secondsFromGMTForDate(NSDate()) return (NSDate(timeInterval: Double(seconds), sinceDate: NSDate())) } 
Sign up to request clarification or add additional context in comments.

4 Comments

can u help me with this question as well @El Captain..? stackoverflow.com/questions/36078975/…
ohk ... I think you got your solution .. :)
@El Captain , can you please help me with this ? stackoverflow.com/questions/36194577/…
I would have added important information in comments*
1

Here is the updated code:

func toLocalTime() -> NSDate { let tz : NSTimeZone = NSTimeZone.defaultTimeZone() let seconds: Int = tz.secondsFromGMTForDate(NSDate()) return (NSDate(timeInterval: Double(seconds), sinceDate: NSDate())) } 

You need to convert to Double

9 Comments

Thank you Sohil. I have another Doubt in my code , but not in this part. Can I ask it here or Should I post another question ?
@ManeeshSharma You can go here!
I have a HomeViewController : UITableViewController and three other controllers namely DetailsViewController : UITableViewController and
AppcatalogViewController: UIViewController. In didSelectRowAtindexpath of homecontroller, i am loading the otherControllers using if (indexPath.section == 0){ var enrolledView = DetailsViewController() self.navigationController?.pushViewController(enrolledView, animated: true) }
but when i load AppCatalogcontroller, which is inherited from UIViewController, error is shown as "Use of Unresolved Identifier 'AppCatalogViewController'". Please help with how should I load it
|
0

Cast seconds:Int into Double

func toLocalTime() -> NSDate { var tz : NSTimeZone = NSTimeZone.defaultTimeZone() var seconds: Int = tz.secondsFromGMTForDate(NSDate()) return (NSDate(timeInterval: Double(seconds), sinceDate: NSDate())) } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.