需要一些帮助才能获得JSON的值.我想访问radioButton对象,并访问description值(例如,竞争对手的其他产品有更好的产品),以分配给tileView.title,如下所示:

 for viewContent in viewModel.contents {
            switch viewContent {
            case let .radioButtonGroup(radioButtonGroupModel):
                for i in 0..<radioButtonGroupModel.options.count {
                    let tileView = TileRadio()
                    tileView.title = radioButtonGroupModel.options...

打印此print(radioButtonGroupModel.options[i])将获得:

radioButton(RadioButtonModel(description: "Other Products from Competitors have better product", identifier: "competitorReason"))

以下是JSON解码器的代码库:

enum ComponentModel: Decodable {

    case radioButtonGroup(RadioButtonGroupModel)
    case radioButton(RadioButtonModel)
    case emptyComponent

    enum CodingKeys: String, CodingKey {
        case radioButtonGroup
        case radioButton
    }

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        do {
            switch container.allKeys.first {

            case .radioButtonGroup:
                let value = try container.decode(RadioButtonGroupModel.self, forKey: .radioButtonGroup)
                self = .radioButtonGroup(value)
            case .radioButton:
                let value = try container.decode(RadioButtonModel.self, forKey: .radioButton)
                self = .radioButton(value)
            case .none:
                self = .emptyComponent
            }
        } catch {
            self = .emptyComponent
        }
    }
}

struct RadioButtonGroupModel: Decodable {
    let options: [ComponentModel]
}

struct RadioButtonModel: Decodable {
    let description: String
    let identifier: String
}

以下是部分JSON文件:

"closureReasons": {
        "pageTitle": "Close account",
        "content": [
 
            {
                "heading": {
                    "title": "Why are you closing this account?"
                }
            },
            {
                "spacing": ".spacing3x"
            },
            {
                "radioButtonGroup": {
                    "options": [
                        {
                            "radioButton": {
                                "identifier": "competitorReason",
                                "description": "Other Products from Competitors have better product"
                            }
                        },
                        {
                            "radioButton": {
                                "identifier": "betterReason",
                                "description": "I found a better product"
                            }
                        },

你对如何检索单选按钮属性有什么 idea 吗?非常感谢您的帮助

推荐答案

for个循环也可以与case let个循环进行模式匹配:

for case let .radioButton(radioButtonModel) in radioButtonGroupModel.options {
    print(radioButtonModel.description)
}

这个循环遍历options中的所有ComponentModel,也就是.radioButton.

或者,如果您出于某种原因需要索引:

for case let (i, .radioButton(radioButtonModel)) in radioButtonGroupModel.options.enumerated() {
    print(i, radioButtonModel.description)
}

也就是说,如果RadioButtonGroupModel.options只能包含.radioButton个大小写,我建议将其类型改为[RadioButtonModel],并添加一些自定义解码逻辑来解码它:

struct RadioButtonGroupModel: Decodable {
    let options: [RadioButtonModel]
    
    enum OptionsCodingKey: String, CodingKey {
        case options
    }
    
    
    enum RadioButtonCodingKey: String, CodingKey {
        case radioButton
    }
    
    init(from decoder: Decoder) throws {
        var container = try decoder.container(keyedBy: OptionsCodingKey.self)
        var arrayContainer = try container.nestedUnkeyedContainer(forKey: .options)
        var options = [RadioButtonModel]()
        while !arrayContainer.isAtEnd {
            var radioButtonContainer = try arrayContainer.nestedContainer(keyedBy: RadioButtonCodingKey.self)
            if let model = radioButtonContainer.decodeIfPresent(RadioButtonModel.self, forKey: .radioButton) {
                options.append(model)
            }
        }
        self.options = options
        
    }
}

Ios相关问答推荐

彩色进度线--SWIFT

如何根据SWIFT中的按钮点击更新文本值

分配给指针会通过 class_addMethod 导致 EXC_BAD_ACCESS

如何在后台显示 Swift UI 中的通讯通知?

mapView swift 导致警告有网格错误

在 SwiftUI 中以编程方式插入内嵌图像

Swift Combine 防止初始值触发接收器并同时防止重复?

我需要二进制文件来进行 App Store 应用程序传输吗?

有没有办法访问自动生成的 Codable 一致性的编码键?

类型任何视图不能符合具有泛型的协议上的视图

AVPlayer 退出全屏

使用 CIImage 支持的 UIImage 设置 UIImageView 时发生罕见的崩溃

UIImageView 使用 CGAffineTransform 错误旋转

SignalR 永远不会在 iOS 上使用 Xamarin Forms 启动

如何快速设置条形按钮的图像?

以编程方式获取故事板 ID?

核心数据 - 无法在路径加载优化模型

有什么方法可以加粗 NSString 的一部分?

如何使用 AFNetworking 设置超时

在 NSUserDefaults 中存储值是否有任何限制?