Retrieve value from known keys in simple nested Dictionary

I have a dictionary of Strings, like chapters or table of contents for an aircraft manual, like this:

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"]
]

First level string pair is the chapter number and title, second level is each chapters code and its associated aircraft system. The dictionary will be quite long (this is just a few rows from it) but the format is the same so I don’t see a reason for a struct, given its simple construction.

Now, I will have a known exact system code – something like "21-52-30A" and with that String I need to find and return:

  1. the chapter name, e.g. “Air Conditioning” (value) from the top level value because the first 2 characters are 21, and

  2. the system name, e.g. “Air cooling system” (value) from the second level because the first 4 characters are “21-52”.

In other words, how do I match the key from the top level to the first 2 characters in my known string, and the key from the second level to the first 4 characters, and return their respective values?

I’ve done this so far:

for (keys, values) in ataChapter {
    for (item) in keys {
        print(item.value)
    }

    for (chapter) in values {
        print(chapter.value)
    }
}

I understand the concept of this simple loop, and with it I obviously get all the string values back but I’m a beginner at this stuff and can’t figure out the matching part and return only the values matching the code in my String.

5

If you insist on keeping your dictionary structure here is one way to do it

let chapterKey = "21"
let sectionKey = "21-52"

if let chapter = ataChapter.first(where: { $0.key[chapterKey] != nil }),
   let section = chapter.value.first(where: { $0.key == sectionKey }) {
    print(chapter.key.values.first!)
    print(section.key, section.value)
}

Air Conditioning
21-52 Air cooling system

If you don’t like the forced unwrapping you could include

let chapterTitle = chapter.key.values.first,

in the if condition and use that variable instead.

Another solution is to use custom types instead of the dictionary construction. For example

struct Section {
    let key: String
    let title: String
}

struct Chapter {
    let key: String
    let title: String
    let sections: [Section]
}

and then have an array of Chapter. The search code would be pretty similar to what we already have but the advantage of a custom structure is that we can add functionality to it so for instance implement a subscript in Chapter:

subscript(_ sectionKey: String) -> Section? {
    sections.first(where: { $0.key == sectionKey })
}

If we also use a dictionary instead of an array with the chapter key as key

let chapters: [String: Chapter] = ...

then the search code could be simplified to

if let chapter = chapters[chapterKey], let section = chapter[sectionKey] {
    print(chapter.title)
    print(section.key, section.title)
}

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật