swift 很久以来我无法解决的崩溃

u7up0aaq  于 2023-01-16  发布在  Swift
关注(0)|答案(1)|浏览(367)

这是什么出现在崩溃日志为crashlytics:

Crashed: com.apple.main-thread
0  FieldService                   0x665cc specialized MonthNameFormater.stringForValue(_:axis:) + 4304774604 (GraphViewController.swift:4304774604)
1  FieldService                   0x5fd74 @objc MonthNameFormater.stringForValue(_:axis:) + 4304747892 (:4304747892)
2  Charts                         0xcc4c AxisBase.getFormattedLabel(_:) + 149 (AxisBase.swift:149)
3  Charts                         0xcaa8 AxisBase.getLongestLabel() + 138 (AxisBase.swift:138)
4  Charts                         0xb0c78 XAxisRenderer.computeSize() + 158 (XAxisRenderer.swift:158)
5  Charts                         0xb0b3c XAxisRenderer.computeAxisValues(min:max:) + 154 (XAxisRenderer.swift:154)
6  Charts                         0x1cb9c BarLineChartViewBase.notifyDataSetChanged() + 592 (:592)
7  Charts                         0x57ee8 ChartViewBase.data.didset + 656 (:656)
8  Charts                         0x54750 ChartViewBase.data.setter + 84 (:84)
9  FieldService                   0x62368 GraphViewController.updateGraph() + 4304757608 (:4304757608)
10 FieldService                   0x610e0 closure #1 in closure #1 in GraphViewController.viewDidLoad() + 4304752864
11 FieldService                   0xb2000 thunk for @escaping @callee_guaranteed () -> () + 4305084416 (:4305084416)
12 libdispatch.dylib              0x1e6c _dispatch_call_block_and_release + 32
13 libdispatch.dylib              0x3a30 _dispatch_client_callout + 20
14 libdispatch.dylib              0x11f48 _dispatch_main_queue_drain + 928
15 libdispatch.dylib              0x11b98 _dispatch_main_queue_callback_4CF + 44
16 CoreFoundation                 0x51800 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 16
17 CoreFoundation                 0xb704 __CFRunLoopRun + 2532
18 CoreFoundation                 0x1ebc8 CFRunLoopRunSpecific + 600
19 GraphicsServices               0x1374 GSEventRunModal + 164
20 UIKitCore                      0x514b58 -[UIApplication _run] + 1100
21 UIKitCore                      0x296098 UIApplicationMain + 364
22 FieldService                   0x6b74 main + 20 (AppDelegate.swift:20)
23 ???                            0x1018a9da4 (Missing)

正如您所看到的,许多用户都受到了影响。
这是我的类MonthNameFormatter。问题是什么?在哪里?

import Charts
import CoreData
class MonthNameFormater: IndexAxisValueFormatter {
    override func stringForValue(_ value: Double, axis: AxisBase?) -> String {
        let symbols = Calendar.localizedCurrent.shortMonthSymbols
        let index = min(Options.shared.isTheocraticYearEnabled ? (Int(value) + 8) % 12 : Int(value), 11)
        guard symbols.count == 12 else {
            return "-"
        }
        return symbols[index]
    }
}

我在代码中的什么地方使用它?

private let chartView: LineChartView = {
    let view = LineChartView()
    view.xAxis.labelPosition = .bottom
    view.rightAxis.enabled = false
    view.borderLineWidth = 0
    view.xAxis.valueFormatter = MonthNameFormater()
    view.xAxis.granularity = 1
    view.isUserInteractionEnabled = false
    return view
}()

这是一个来自Xcode的信息

Thread 0 name:
Thread 0 Crashed:
0   FieldService                    0x0000000104f665cc Swift runtime failure: Index out of range + 0 (:0)
1   FieldService                    0x0000000104f665cc specialized _ArrayBuffer._checkInoutAndNativeTypeCheckedBounds(_:wasNativeTypeChecked:) + 0 (:0)
2   FieldService                    0x0000000104f665cc specialized Array._checkSubscript(_:wasNativeTypeChecked:) + 0 (GraphViewController.swift:0)
3   FieldService                    0x0000000104f665cc specialized Array.subscript.getter + 0 (GraphViewController.swift:17)
4   FieldService                    0x0000000104f665cc specialized MonthNameFormater.stringForValue(_:axis:) + 744
5   FieldService                    0x0000000104f66498 specialized MonthNameFormater.stringForValue(_:axis:) + 436 (:0)
6   FieldService                    0x0000000104f5fd74 MonthNameFormater.stringForValue(_:axis:) + 4 (:0)
7   FieldService                    0x0000000104f5fd74 @objc MonthNameFormater.stringForValue(_:axis:) + 16
8   Charts                          0x0000000106b88c4c AxisBase.getFormattedLabel(_:) + 160 (AxisBase.swift:149)
9   Charts                          0x0000000106b88aa8 implicit closure #2 in implicit closure #1 in AxisBase.getLongestLabel() + 8 (AxisBase.swift:138)
10  Charts                          0x0000000106b88aa8 thunk for @escaping @callee_guaranteed (@unowned Int) -> (@owned String) + 8 (:0)
11  Charts                          0x0000000106b88aa8 specialized Optional.map(_:) + 8 (:0)
12  Charts                          0x0000000106b88aa8 specialized LazyMapSequence.Iterator.next() + 8 (:0)
13  Charts                          0x0000000106b88aa8 specialized protocol witness for IteratorProtocol.next() in conformance LazyMapSequence.Iterator + 8 (:0)

我不确定这是否是完全相同的bug,但它也与MonthNameFormater有关。

vwkv1x7d

vwkv1x7d1#

给定
Swift runtime failure: Index out of range + 0 (:0)
我怀疑崩溃发生在return symbols[index]-确保index实际上是symbols中的有效索引。

相关问题