将变量从Obj-C转换为Swift

时间:2015-06-24 20:23:42

标签: ios objective-c swift nsnumber nscalendar

我想将这两个变量转换为Swift语言,但我似乎无法找到最好的方法来实现这一点,dateWithTimeIntervalSinceNow在swift中的工作方式不同。

NSNumber *startDate = @((unsigned long long)([[NSDate dateWithTimeIntervalSinceNow:-(60*60)] timeIntervalSince1970]*10000000));
NSNumber *endDate = @((unsigned long long)([[NSDate date] timeIntervalSince1970]*10000000));

到目前为止我的尝试:

let calendar = NSCalendar.currentCalendar()
let oneDayAgo = calendar.dateByAddingUnit(.CalendarUnitDay, value: -1, toDate: NSDate(), options: nil)
var startNum:NSNumber = oneDayAgo!.timeIntervalSince1970*10000000`

1 个答案:

答案 0 :(得分:0)

这大致是你想要做的吗?

let startInterval = NSDate(timeIntervalSinceNow: -(60*60)).timeIntervalSince1970
let endInterval = NSDate().timeIntervalSince1970

print("start: \(startInterval) end: \(endInterval)")

let actualStart = NSDate(timeIntervalSince1970: startInterval)
let actualEnd = NSDate(timeIntervalSince1970: endInterval)

print("one hour ago: \(actualStart) now: \(actualEnd)")