系统重新启动后,我在获取NSTimeInterval
时遇到问题。这个函数给了我一个错误的结果。重新启动时间是在设备的电池充电后重新启动。iPhone 5s和iPad 3(新款)出现问题。我该怎么修?
我使用这个方法,它帮助我
public static func uptime() -> (days: Int, hrs: Int, mins: Int, secs: Int) {
var currentTime = time_t()
var bootTime = timeval()
var mib = [CTL_KERN, KERN_BOOTTIME]
// NOTE: Use strideof(), NOT sizeof() to account for data structure
// alignment (padding)
// http://stackoverflow.com/a/27640066
// https://devforums.apple.com/message/1086617#1086617
var size = strideof(timeval)
let result = sysctl(&mib, u_int(mib.count), &bootTime, &size, nil, 0)
if result != 0 {
#if DEBUG
print("ERROR - (__FILE__):(__FUNCTION__) - errno = "
+ "(result)")
#endif
return (0, 0, 0, 0)
}