使用具有较大差异的Datecomponents时崩溃 [英] Crash while using Datecomponents having larger differences
问题描述
尝试在swift中使用日期组件时,日期之间的间隔很大(例如使用的日期是1979年8月16日到2053年11月11日)
日期组件的代码是
Crashing for large interval between dates while trying to use date components in swift (for example dates used is 16 August 1979 to 11 may 2053) The code for date components is
ComponentsforDetail = (Calendar.current as NSCalendar).components([.second], from: FromDateValue!, to: ToValueDate!, options: [])
使用断点我发现thread_exc发生在装配线代码中
Using breakpoints i found out the thread_exc happens in an assembly line code
libswiftFoundation.dylib`static Foundation.DateComponents._unconditionallyBridgeFromObjectiveC (Swift.Optional<__ObjC.NSDateComponents>) -> Foundation.DateComponents:
0x16cf280 <+0>: push {r4, r5, r7, lr}
0x16cf284 <+4>: add r7, sp, #8
0x16cf288 <+8>: sub sp, sp, #4
0x16cf28c <+12>: mov r4, r0
0x16cf290 <+16>: mov r0, #0
0x16cf294 <+20>: str r0, [sp]
0x16cf298 <+24>: cmp r4, #0
0x16cf29c <+28>: beq 0x16cf2dc ; <+92>
0x16cf2a0 <+32>: mov r0, r4
0x16cf2a4 <+36>: bl 0x17a42e8 ; symbol stub for: objc_retain
0x16cf2a8 <+40>: mov r1, sp
0x16cf2ac <+44>: mov r0, r4
0x16cf2b0 <+48>: bl 0x174ed80 ; function signature specialization <Arg[0] = Owned To Guaranteed, Arg[2] = Dead> of static Foundation.DateComponents._conditionallyBridgeFromObjectiveC (__ObjC.NSDateComponents, result : inout Swift.Optional<Foundation.DateComponents>) -> Swift.Bool
0x16cf2b4 <+52>: mov r0, r4
0x16cf2b8 <+56>: bl 0x17a42d8 ; symbol stub for: objc_release
0x16cf2bc <+60>: ldr r5, [sp]
0x16cf2c0 <+64>: cmp r5, #0
0x16cf2c4 <+68>: beq 0x16cf2e0 ; <+96>
0x16cf2c8 <+72>: mov r0, r4
0x16cf2cc <+76>: bl 0x17a42d8 ; symbol stub for: objc_release
0x16cf2d0 <+80>: mov r0, r5
0x16cf2d4 <+84>: sub sp, r7, #8
0x16cf2d8 <+88>: pop {r4, r5, r7, pc}
-> 0x16cf2dc <+92>: trap (Here is the breakpoint exception Happening)
0x16cf2e0 <+96>: trap
我认为这是一个整数格式的问题,因为大于68年的任何事情导致Datecomponents中的崩溃几秒钟
我使用Playground来识别操场中使用的代码的问题
I think this is an issue with integer format because anything greater than 68 years is causing this crash in Datecomponents for seconds I've used Playground to identify the issue the code used in playground is
import UIKit
var str = "Hello, playground"
let dateString1 = "1979-11-28 07:57:20 +0000"
let dateString2 = "2048-11-29 10:59:00 +0000"
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
let date1 = formatter.date(from: dateString1)!
let date2 = formatter.date(from: dateString2)!
let calendar = NSCalendar.current
let components1 = (Calendar.current as NSCalendar).components([NSCalendar.Unit.minute], from: date1, to: date2, options: NSCalendar.Options.init(rawValue: 0))
let components2 = (Calendar.current as NSCalendar).components([.second], from: date1, to: date2, options: [])
date2大于2047年的任何内容都会导致此错误
Anything for date2 greater than year 2047 will result this error
推荐答案
当使用 dateComponents
Calendar
获取时,问题似乎发生了两个日期之间的秒数和结果的秒数太大而不适合签名的32位整数(2 ^ 31)。
The problem seems to happen when the dateComponents
of Calendar
is used to get the number of seconds between two dates and the resulting number of seconds is too large to fit in a signed 32-bit integer (2^31).
另一种解决方案是使用日期timeIntervalSince
来获取两个日期之间的秒数。
An alternate solution is to use Date timeIntervalSince
to get the number of seconds between two dates.
您的更新代码变为:
let dateString1 = "1979-11-28 07:57:20 +0000"
let dateString2 = "2048-11-29 10:59:00 +0000"
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
let date1 = formatter.date(from: dateString1)!
let date2 = formatter.date(from: dateString2)!
let seconds = date2.timeIntervalSince(date1)
let components1 = Calendar.current.dateComponents([ .minute ], from: date1, to: date2)
let minutes = components1.minute
这给了 2177636500
秒和 36293941
分钟。
这篇关于使用具有较大差异的Datecomponents时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!