【问题标题】:Unable to view a passing SwiftUI parameter value via debugger无法通过调试器查看传递的 SwiftUI 参数值
【发布时间】:2020-11-02 21:20:37
【问题描述】:

场景:我想在调试器中查看CountryRegionListModel()的一个参数。
以下是视图及其依赖模型 sn-ps:

import Combine
import UIKit

protocol URLResource {
    associatedtype DataModel: Decodable
    var url: URL? { get }
}

struct CovidResource<T: Decodable>: URLResource {
    typealias DataModel = T
    var url = URL(string: "https://disease.sh/v3/covid-19/apple/countries/Canada")
}

// =====================================================================================================

class CountryRegionListModel: ObservableObject {
    @Published var countryRegionList: [String] = []

    // Data Persistence:
    var cancellables: Set<AnyCancellable> = []

    // ---------------------------------------------------------------------------

    func getList<Resource>(urlDataModel: Resource) where Resource: URLResource {
        let remoteDataPublisher = URLSession.shared.dataTaskPublisher(for: urlDataModel.url!)
            .map(\.data)
            .receive(on: DispatchQueue.main)
            .decode(type: Resource.DataModel.self, decoder: JSONDecoder())

        remoteDataPublisher
            .sink(receiveCompletion: { completion in
                switch completion {
                case .finished:
                    print("Publisher Finished")
                case let .failure(anError):
                    Swift.print("received error: ", anError)
                }
            }, receiveValue: { [self] someValue in
                self.countryRegionList = someValue as! [String]
            }).store(in: &cancellables)
    }
}

在这里,我调用了一个子选择器视图来显示注入其中的数据。
我想通过调试器检查这些数据:countryListViewModel.countryRegionList

CountryRegionPickerView(countryRegionList: $countryListViewModel.countryRegionList)
                            

我不明白为什么会这样。
如何检查是否有数据传递到子视图?

【问题讨论】:

    标签: swift swiftui lldb


    【解决方案1】:

    由于您的countryListViewModel 存储在属性包装器中,因此没有名为countryListViewModel 的存储属性。相反,存储的属性被命名为_countryListViewModel。调试器不理解这一点(可能是因为编译器在调试信息中没有正确解释它)。

    countryListViewModel 属性实际上是一个计算属性,它的 getter 本质上只是_countryListViewModel.wrappedValue。所以试试这个:

    po _countryListViewModel.wrappedValue.countryRegionList
    

    或者(因为countryRegionList 也是一个包装属性)可能是这样的:

    po _countryListViewModel.wrappedValue._countryRegionList.wrappedValue
    

    【讨论】:

    • 我在这里学到了一些东西。您的第一个选择有效;不是第二个。
    猜你喜欢
    • 2022-01-02
    • 2021-11-19
    • 1970-01-01
    • 2011-12-18
    • 2021-09-20
    • 2020-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多