gpt4 book ai didi

ios - 使用具有较大差异的 Datecomponents 时崩溃

转载 作者:可可西里 更新时间:2023-11-01 02:04:56 29 4
gpt4 key购买 nike

尝试在 swift 中使用日期组件时,日期之间的较大间隔会崩溃(例如,使用的日期是 1979 年 8 月 16 日至 2053 年 5 月 11 日)日期组件的代码是

ComponentsforDetail = (Calendar.current as NSCalendar).components([.second], from: FromDateValue!, to: ToValueDate!, options: [])

使用断点我发现 thread_exc 发生在流水线代码中

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 来识别 playground 中使用的代码的问题

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 年的任何内容都会导致此错误

最佳答案

问题似乎发生在 CalendardateComponents 用于获取两个日期之间的秒数并且产生的秒数太大而无法容纳带符号的 32 位整数 (2^31)。

另一种解决方案是使用 Date timeIntervalSince 获取两个日期之间的秒数。

更新后的代码变为:

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 分钟。

关于ios - 使用具有较大差异的 Datecomponents 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43876695/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com