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 !

Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
AnxiousMan
  • 578
  • 2
  • 8
  • 25
  • can you not just go `var seconds: NSTimeInterval = tz.secondsFromGMTForDate(NSDate())` ? – Fonix Mar 16 '16 at 06:03

3 Answers3

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()))
 } 
Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
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

Sohil R. Memon
  • 9,404
  • 1
  • 31
  • 57
  • 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 ? – AnxiousMan Mar 16 '16 at 06:13
  • @ManeeshSharma You can go here! – Sohil R. Memon Mar 16 '16 at 06:15
  • I have a HomeViewController : UITableViewController and three other controllers namely DetailsViewController : UITableViewController and – AnxiousMan Mar 16 '16 at 06:16
  • 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) } – AnxiousMan Mar 16 '16 at 06:18
  • 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 – AnxiousMan Mar 16 '16 at 06:19
  • @ManeeshSharma Can I see that code of how you load `AppCatalogViewController`? – Sohil R. Memon Mar 16 '16 at 06:22
  • if (indexPath.section == 1){ var appcatalog = AppCatalogViewController() self.navigationController?.pushViewController(appcatalog, animated: true) } – AnxiousMan Mar 16 '16 at 06:25
  • @ManeeshSharma You are doing it in a wrong way. You are creating a wrong instance. Refer to this answer: `let vc = storyboard?.instantiateViewControllerWithIdentifier("MyViewController") as MyCustomViewController presentViewController(vc, animated: true, completion: nil)` http://stackoverflow.com/questions/13867565/what-is-a-storyboard-id-and-how-can-i-use-this – Sohil R. Memon Mar 16 '16 at 06:27
  • I Haven't used a StoryBoard in my Project Sohil – AnxiousMan Mar 16 '16 at 06:29
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()))
}
Muzahid
  • 5,072
  • 2
  • 24
  • 42