You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift嵌套字典检索:根据系统编码前缀获取章节与系统名称

ATA编码匹配嵌套字典的实现

原字典结构

let ataChapter: [[String:String]:Dictionary] = [
    ["21":"Air Conditioning"] : [
        "21-31":"Pressure control and monitoring",
        "21-51":"Pack flow control",
        "21-52":"Air cooling system",
        "21-55":"Emergency ram air inlet",
        "21-61":"Pack temperature control",
        "21-63":"Cockpit and cabin temperature control"],
    ["22":"Autoflight"] : [
        "22-01":"Overhead panel",
        "22-05":"FMA",
        "22-10":"A/P and F/D",
        "22-30":"Autothrust",
        "22-83":"FMGC"]
]

需求实现

1. 根据编码前2位获取章节名称

提取编码前2位作为目标章节编码,遍历顶级字典的键(小型[String:String]字典),匹配到对应编码后返回章节名称:

func getChapterName(from code: String) -> String? {
    // 校验编码长度,提取前2位章节编码
    guard code.count >= 2 else { return nil }
    let targetChapterCode = String(code.prefix(2))
    
    // 遍历顶级字典匹配
    for (chapterEntry, _) in ataChapter {
        if let chapterCode = chapterEntry.keys.first, chapterCode == targetChapterCode {
            return chapterEntry.values.first
        }
    }
    return nil
}

// 调用示例
if let chapterName = getChapterName(from: "21-52-30A") {
    print(chapterName) // 输出:Air Conditioning
}

2. 根据编码前4位获取系统名称

提取编码前4位作为目标系统编码,遍历所有二级字典,匹配到对应编码后返回系统名称:

func getSystemName(from code: String) -> String? {
    // 校验编码长度,提取前4位系统编码
    guard code.count >= 4 else { return nil }
    let targetSystemCode = String(code.prefix(4))
    
    // 遍历二级字典匹配
    for (_, systemDictionary) in ataChapter {
        if let systemName = systemDictionary[targetSystemCode] as? String {
            return systemName
        }
    }
    return nil
}

// 调用示例
if let systemName = getSystemName(from: "21-52-30A") {
    print(systemName) // 输出:Air cooling system
}

结构优化建议

原顶级字典使用[String:String]作为键,遍历匹配效率较低。建议调整为更直观高效的结构,无需使用struct,只需拆分出章节名称映射和嵌套字典:

// 章节编码与名称的映射表
let chapterNames: [String: String] = [
    "21": "Air Conditioning",
    "22": "Autoflight"
]

// 优化后的嵌套字典
let ataChapterOptimized: [String: [String: String]] = [
    "21": [
        "21-31": "Pressure control and monitoring",
        "21-51": "Pack flow control",
        "21-52": "Air cooling system",
        "21-55": "Emergency ram air inlet",
        "21-61": "Pack temperature control",
        "21-63": "Cockpit and cabin temperature control"
    ],
    "22": [
        "22-01": "Overhead panel",
        "22-05": "FMA",
        "22-10": "A/P and F/D",
        "22-30": "Autothrust",
        "22-83": "FMGC"
    ]
]

优化后的匹配函数更简洁高效:

// 优化后的章节名称获取
func getChapterNameOptimized(from code: String) -> String? {
    guard code.count >= 2 else { return nil }
    let targetChapterCode = String(code.prefix(2))
    return chapterNames[targetChapterCode]
}

// 优化后的系统名称获取
func getSystemNameOptimized(from code: String) -> String? {
    guard code.count >= 4 else { return nil }
    let targetChapterCode = String(code.prefix(2))
    let targetSystemCode = String(code.prefix(4))
    return ataChapterOptimized[targetChapterCode]?[targetSystemCode]
}

内容的提问来源于stack exchange,提问作者ScoobyDooku

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 01:22:37