了解崩溃日志中的iOS堆栈跟踪



我有一个奇怪的情况,我的应用程序加载它的应用商店视图在测试中很好,包括通过testflight,但从appstore下载时它崩溃了。我在下面找到了相关的崩溃日志,但我不确定哪些部分与我的代码有关,哪些部分与引擎盖下发生的事情有关。

这是堆栈跟踪:

Thread 4 Crashed:
0   My app          0x00000001001b7908 Swift runtime failure: Index out of range + 0 (<compiler-generated>:0)
1   My app          0x00000001001b7908 subscript.get + 20 (<compiler-generated>:0)
2   My app          0x00000001001b7908 specialized Store.productsRequest(_:didReceive:) + 1376
3   My app          0x00000001001b75e8 list.get + 28 (Store.swift:0)
4   My app          0x00000001001b75e8 specialized Store.productsRequest(_:didReceive:) + 576
5   My app          0x00000001001b5d8c productsRequest + 12 (<compiler-generated>:0)
6   My app          0x00000001001b5d8c @objc Store.productsRequest(_:didReceive:) + 76
7   StoreKit                        0x00000001ab0f8070 __27-[SKProductsRequest _start]_block_invoke_2 + 136 (SKProductsRequest.m:104)
8   libdispatch.dylib               0x000000018f5404b4 _dispatch_call_block_and_release + 32 (init.c:1518)
9   libdispatch.dylib               0x000000018f541fdc _dispatch_client_callout + 20 (object.m:560)
10  libdispatch.dylib               0x000000018f5450c8 _dispatch_queue_override_invoke + 788 (inline_internal.h:2632)
11  libdispatch.dylib               0x000000018f553a6c _dispatch_root_queue_drain + 396 (inline_internal.h:0)
12  libdispatch.dylib               0x000000018f554284 _dispatch_worker_thread2 + 164 (queue.c:7052)
13  libsystem_pthread.dylib         0x00000001d49e4dbc _pthread_wqthread + 228 (pthread.c:2631)
14  libsystem_pthread.dylib         0x00000001d49e4b98 start_wqthread + 8

从这里我读到有一些东西在调用productsRequest出错,所以这里是这个函数:

    func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
        let myProduct = response.products
        for product in myProduct {
            list.append(product)
        }
        // Update labels
        localTitle = list[0].localizedTitle
        localDescription = list[0].localizedDescription
        
        // Format the price and display
        let formatter = NumberFormatter()
        formatter.locale = Locale.current
        formatter.numberStyle = .currency
        if let formattedPrice = formatter.string(from: list[0].price){
            localPrice = ("Upgrade: (formattedPrice)")
            delegate?.storeUpdateReceived(store: self)
        }
    }

这是我不确定的。我们能确定这个函数的问题在哪里吗?还是在我们实际进入函数内的行之前触发了这个问题?我无法在运行环境之外重现崩溃,所以我认为我不能在运行安装的应用程序中添加断点。

我看到索引超出范围错误,但这可能是由我引用列表中的第一个元素(可能是空的)引起的吗?或者这个指标是别的什么?

从可用的信息来看,我们可以假设它在引用list的第一个元素的行上崩溃了。如果列表为空,它肯定会崩溃,您可以这样写:

 func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
        let myProduct = response.products
        for product in myProduct {
            list.append(product)
        }
        // Update labels
        localTitle = list.first?.localizedTitle ?? ""
        localDescription = list.first?.localizedDescription ?? ""
        
        // Format the price and display
        let formatter = NumberFormatter()
        formatter.locale = Locale.current
        formatter.numberStyle = .currency
        if let price = list.first?.price, let formattedPrice = formatter.string(from: price){
            localPrice = ("Upgrade: (formattedPrice)")
            delegate?.storeUpdateReceived(store: self)
        }
    }

最新更新