一直可靠工作的时间戳函数刚刚导致 EXC_BAD_INSTRUCTION
Timestamp function that has been working reliably just caused EXC_BAD_INSTRUCTION
我一直在使用这个函数来生成时间戳。我在 Stack Overflow 的某个地方找到了它。
@objc public class var timestamp: String {
return "\(Int(NSDate().timeIntervalSince1970 * 1000))"
}
它一直没有问题,但我刚收到 EXC_BAD_INSTRUCTION:
fatal error: floating point value cannot be converted to Int because it is greater than Int.max
随着这个应用程序的开发接近完成,突然看到它引起 EXC_BAD_INSTRUCTION 让我很紧张。这是模拟器中的 运行,但我设置了有效的日期和时间。
如有任何想法或建议,我们将不胜感激。下面是回溯。
(lldb) bt
* thread #1: tid = 0x15c4d, 0x04a30393 libswiftCore.dylib`function signature specialization <Arg[0] = Exploded, Arg[1] = Exploded, Arg[2] = Dead, Arg[3] = Dead> of Swift._fatalErrorMessage (Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt) -> () + 67, queue = 'com.apple.main-thread', stop reason = EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
frame #0: 0x04a30393 libswiftCore.dylib function signature specialization <Arg[0] = Exploded, Arg[1] = Exploded, Arg[2] = Dead, Arg[3] = Dead> of Swift._fatalErrorMessage (Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt) -> () + 67
* frame #1: 0x00afe0c9 TDTPhotoLib static
TDTDeviceUtilites.timestamp.getter(self=TDTPhotoLib.TDTDeviceUtilites) + 409 at TDTDeviceUtilites.swift:127
frame #2: 0x00166979 Oilist TDTPaintingViewController.finshedSession(sender=0x7aee0fb0, self=0x7e9d3800) -> () + 169 at TDTPaintingViewController.swift:1370
frame #3: 0x00102896 Oilist TDTOilistMenuPainting.goForward(sender=0x7aee0fb0, self=0x7c218770) -> () + 374 at TDTOilistMenuPainting.swift:149
frame #4: 0x0010290d Oilist @objc TDTOilistMenuPainting.goForward(UIButton!) -> () + 61 at TDTOilistMenuPainting.swift:0
frame #5: 0x018a80b5 libobjc.A.dylib -[NSObject performSelector:withObject:withObject:] + 84
frame #6: 0x0336ee38 UIKit -[UIApplication sendAction:to:from:forEvent:] + 118
frame #7: 0x0336edb7 UIKit -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 64
frame #8: 0x03512f3b UIKit -[UIControl sendAction:to:forEvent:] + 79
frame #9: 0x035132d4 UIKit -[UIControl _sendActionsForEvents:withEvent:] + 433
frame #10: 0x035122c1 UIKit -[UIControl touchesEnded:withEvent:] + 714
frame #11: 0x033ef52e UIKit -[UIWindow _sendTouchesForEvent:] + 1095
frame #12: 0x033f05cc UIKit -[UIWindow sendEvent:] + 1159
frame #13: 0x03391be8 UIKit -[UIApplication sendEvent:] + 266
frame #14: 0x03366769 UIKit _UIApplicationHandleEventQueue + 7795
frame #15: 0x02423e5f CoreFoundation __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
frame #16: 0x02419aeb CoreFoundation __CFRunLoopDoSources0 + 523
frame #17: 0x02418f08 CoreFoundation __CFRunLoopRun + 1032
frame #18: 0x02418846 CoreFoundation CFRunLoopRunSpecific + 470
frame #19: 0x0241865b CoreFoundation CFRunLoopRunInMode + 123
frame #20: 0x07031664 GraphicsServices GSEventRunModal + 192
frame #21: 0x070314a1 GraphicsServices GSEventRun + 104
frame #22: 0x0336ceb9 UIKit UIApplicationMain + 160
frame #23: 0x00131ed1 Oilist main + 145 at AppDelegate.swift:14
frame #24: 0x05631a25 libdyld.dylib start + 1
您的代码将在所有 32 位平台上崩溃(例如 iPhone 4、5)
因为
的结果
NSDate().timeIntervalSince1970 * 1000
// E.g.: 1464850525047.38
不适合 32 位整数。作为解决方案,使用 64 位整数:
Int64(NSDate().timeIntervalSince1970 * 1000)
或者,如果打算创建一个 string 表示
毫秒,使用字符串格式而不是整数转换:
String(format:"%.0f", NSDate().timeIntervalSince1970 * 1000)
我一直在使用这个函数来生成时间戳。我在 Stack Overflow 的某个地方找到了它。
@objc public class var timestamp: String {
return "\(Int(NSDate().timeIntervalSince1970 * 1000))"
}
它一直没有问题,但我刚收到 EXC_BAD_INSTRUCTION:
fatal error: floating point value cannot be converted to Int because it is greater than Int.max
随着这个应用程序的开发接近完成,突然看到它引起 EXC_BAD_INSTRUCTION 让我很紧张。这是模拟器中的 运行,但我设置了有效的日期和时间。
如有任何想法或建议,我们将不胜感激。下面是回溯。
(lldb) bt
* thread #1: tid = 0x15c4d, 0x04a30393 libswiftCore.dylib`function signature specialization <Arg[0] = Exploded, Arg[1] = Exploded, Arg[2] = Dead, Arg[3] = Dead> of Swift._fatalErrorMessage (Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt) -> () + 67, queue = 'com.apple.main-thread', stop reason = EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
frame #0: 0x04a30393 libswiftCore.dylib function signature specialization <Arg[0] = Exploded, Arg[1] = Exploded, Arg[2] = Dead, Arg[3] = Dead> of Swift._fatalErrorMessage (Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt) -> () + 67
* frame #1: 0x00afe0c9 TDTPhotoLib static
TDTDeviceUtilites.timestamp.getter(self=TDTPhotoLib.TDTDeviceUtilites) + 409 at TDTDeviceUtilites.swift:127
frame #2: 0x00166979 Oilist TDTPaintingViewController.finshedSession(sender=0x7aee0fb0, self=0x7e9d3800) -> () + 169 at TDTPaintingViewController.swift:1370
frame #3: 0x00102896 Oilist TDTOilistMenuPainting.goForward(sender=0x7aee0fb0, self=0x7c218770) -> () + 374 at TDTOilistMenuPainting.swift:149
frame #4: 0x0010290d Oilist @objc TDTOilistMenuPainting.goForward(UIButton!) -> () + 61 at TDTOilistMenuPainting.swift:0
frame #5: 0x018a80b5 libobjc.A.dylib -[NSObject performSelector:withObject:withObject:] + 84
frame #6: 0x0336ee38 UIKit -[UIApplication sendAction:to:from:forEvent:] + 118
frame #7: 0x0336edb7 UIKit -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 64
frame #8: 0x03512f3b UIKit -[UIControl sendAction:to:forEvent:] + 79
frame #9: 0x035132d4 UIKit -[UIControl _sendActionsForEvents:withEvent:] + 433
frame #10: 0x035122c1 UIKit -[UIControl touchesEnded:withEvent:] + 714
frame #11: 0x033ef52e UIKit -[UIWindow _sendTouchesForEvent:] + 1095
frame #12: 0x033f05cc UIKit -[UIWindow sendEvent:] + 1159
frame #13: 0x03391be8 UIKit -[UIApplication sendEvent:] + 266
frame #14: 0x03366769 UIKit _UIApplicationHandleEventQueue + 7795
frame #15: 0x02423e5f CoreFoundation __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
frame #16: 0x02419aeb CoreFoundation __CFRunLoopDoSources0 + 523
frame #17: 0x02418f08 CoreFoundation __CFRunLoopRun + 1032
frame #18: 0x02418846 CoreFoundation CFRunLoopRunSpecific + 470
frame #19: 0x0241865b CoreFoundation CFRunLoopRunInMode + 123
frame #20: 0x07031664 GraphicsServices GSEventRunModal + 192
frame #21: 0x070314a1 GraphicsServices GSEventRun + 104
frame #22: 0x0336ceb9 UIKit UIApplicationMain + 160
frame #23: 0x00131ed1 Oilist main + 145 at AppDelegate.swift:14
frame #24: 0x05631a25 libdyld.dylib start + 1
您的代码将在所有 32 位平台上崩溃(例如 iPhone 4、5) 因为
的结果NSDate().timeIntervalSince1970 * 1000
// E.g.: 1464850525047.38
不适合 32 位整数。作为解决方案,使用 64 位整数:
Int64(NSDate().timeIntervalSince1970 * 1000)
或者,如果打算创建一个 string 表示 毫秒,使用字符串格式而不是整数转换:
String(format:"%.0f", NSDate().timeIntervalSince1970 * 1000)