Timestamp function that has been working reliably just caused EXC_BAD_INSTRUCTION

后端 未结 1 878
悲哀的现实
悲哀的现实 2020-12-04 03:33

I have been using this function to generate a time stamp. I found it somewhere here on Stack Overflow.

@objc public class var timestamp: String {
    return          


        
相关标签:
1条回答
  • 2020-12-04 03:41

    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)
    
    0 讨论(0)
提交回复
热议问题