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

watbbzwu  于 2022-11-18  发布在  iOS
关注(0)|答案(1)|浏览(259)

我遇到了一个奇怪的情况,我的应用在测试中(包括通过testflight)加载其应用商店内视图时很好,但从应用商店下载时却崩溃了。我在下面找到了相关的崩溃日志,但我不确定哪些部分与我的代码有关,哪些部分与引擎盖下发生的事情有关。
这是堆栈跟踪:

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)
        }
    }

但我不确定的是,我们能否确定问题出在函数中的什么地方?或者问题是在我们实际进入函数中的行之前触发的?我无法在实际环境之外重现崩溃,所以我认为我不能在实际安装的应用中添加断点。
我看到索引超出范围错误,但这可能是由于我引用列表中第一个元素的位置(可能是空的)引起的吗?或者该索引可能是其他东西?

qacovj5a

qacovj5a1#

根据现有的信息,我们可以假设它在你引用list的第一个元素的那一行崩溃了。如果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)
        }
    }

相关问题