Timestamp function that has been working reliably just caused EXC_BAD_INSTRUCTION
Your code will crash on all 32-bit platforms (such as iPhone 4, 5)
because the result of
NSDate().timeIntervalSince1970 * 1000
// E.g.: 1464850525047.38
does not fit into a 32-bit integer. As a solution, use 64-bit integers:
Int64(NSDate().timeIntervalSince1970 * 1000)
Alternatively, if the intention is to create a string representing
the milliseconds, use string formatting instead of an integer conversion:
String(format:"%.0f", NSDate().timeIntervalSince1970 * 1000)
While creating a String value from timestamp, getting crash [Swift]
Don't cast to int, go to string directly and avoid the 32bit device length error
let time_stamp = String(format:"%.0f", NSDate().timeIntervalSince1970 * 1000)
Why dict[String(aUint16)] = Int(Date().timeIntervalSince1970 * 1000) failed in some cases?
From the docs for Int
:
On 32-bit platforms, Int is the same size as Int32, and on 64-bit platforms, Int is the same size as Int64.
A signed 32-bit integer has a maximum value of 2,147,483,647.
At the moment, Int(Date().timeIntervalSince1970 * 1000)
returns a value of 1,495,855,170,970.
That is significantly larger than what will fit in a 32-bit integer.
The crash is being caused by the overflow you are attempting when casting the Double
to an Int
when run on a 32-bit iOS device.
I would suggest an explicit use of Int64
instead of Int
:
begin[String(sequenceNumber)] = Int64(Date().timeIntervalSince1970 * 1000)
Google App Maker timezone timestamp not using users device information
The users email is the binding "@user.email" if that is allowed for your domain.
Swift: What's wrong with this math function?
The size of Int
is 32-bit on 32-bit platforms (e.g. iPhone 5 and below), but the timestamp nowadays is in the range 0x57300000
, which will certainly overflow when you multiply it by 2. In Swift arithmetic overflow will cause the program to crash.
You could force the output to be an Int64, or just return a TimeInterval
(a Double
) if you don't require the output to be an integer.
// swift 3
func Unixtime() -> TimeInterval {
return Date().timeIntervalSince1970
}
// swift 2
func Unixtime() -> NSTimeInterval {
return NSDate().timeIntervalSince1970
}
print((Unixtime() + 2) * 2)
Related Topics
Location Access - App Is Not Asking for User Permission to Access Location - iOS 11
Tracking Mkmapview Centercoordinate While Panning
Present a Modal View Controller with Transparent Background
Setting Up a Parent-Child Relationship in Core Data
Multiline Uibutton and Autolayout
Exit Application When Click Button - iOS
Nsnumberformatter with Comma Decimal Separator
How Long Does Apple Permit a Background Task to Run
How to Add an Identifier to Auto Layout Constraints in Interface Builder
Uiimagepicker Cameraoverlayview Appears on Retake Screen
How to Draw a Point Using Core Graphics
How to Disable 4 Finger Gestures on iPad
How to Identify iOS Device Uniquely
Sprite Kit Pin Joints Appear to Have an Incorrect Anchor
Wkwebview Causes My View Controller to Leak
How to Change the Blue Highlight Color of a Uitableviewcell
Add a Child View Controller's View to a Subview of the Parent View Controller