首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Swift -如何从value中获取Dictionary元素的索引?

Swift -如何从value中获取Dictionary元素的索引?
EN

Stack Overflow用户
提问于 2018-06-04 02:46:30
回答 4查看 3K关注 0票数 0

我使用Dictionary (Swift 4.1)数组来存储语言名称和语言代码的值:

var voiceLanguagesList: [Dictionary<String, String>] = []

我用这个方法追加了字典数组:

for voice in AVSpeechSynthesisVoice.speechVoices() {
            let voiceLanguageCode = (voice as AVSpeechSynthesisVoice).language

            if let languageName = Locale.current.localizedString(forLanguageCode: voiceLanguageCode) {
                let dictionary = [ControllerConstants.ChooseLanguage.languageName: languageName, ControllerConstants.ChooseLanguage.languageCode: voiceLanguageCode]

                voiceLanguagesList.append(dictionary)
            }
        }

现在假设我在userDefaults中存储了元素的值:

let languageName = UserDefaults.standard.object(forKey: ControllerConstants.UserDefaultsKeys.languageName) as? String
        let languageCode = UserDefaults.standard.object(forKey: ControllerConstants.UserDefaultsKeys.languageCode) as? String

我想获取字典的索引,其值为languageNamelanguageCode。我查看了其他答案,但没有找到任何好的解决方案。

SourceCode:https://github.com/imjog/susi_iOS/tree/voice

文件链接:https://github.com/imjog/susi_iOS/blob/voice/Susi/Controllers/LanguagePickerController/LanguagePickerController.swift

EN

回答 4

Stack Overflow用户

发布于 2018-06-04 02:57:04

字典是无序的。他们没有索引。字典有键,键是一些可以散列的值。

字典中的所有键都是唯一的,但并非所有值都是唯一的。

您可以有一个带有值的[String:Int]类型的字典

["a": 1, "b": 1, "c": 1]

在这种情况下,值1的关键字是什么?任何包含该值的键都可以吗?在这种情况下,您可以遍历键/值对,直到找到匹配值并返回该键,但没有第一个匹配值这样的东西,因为如前所述,字典是无序的。

票数 1
EN

Stack Overflow用户

发布于 2018-06-04 03:01:45

我看不出这里有必要用字典。您可以简单地创建一种新的类型语言并执行搜索,如下所示。

struct Language: Equatable {
    var code: String
    var name: String
}

var languages = [Language]()
languages.append(Language(code: "1", name: "English"))
languages.append(Language(code: "2", name: "Hindi"))

let languageToSearch = Language(code: "2", name: "Hindi")

print(languages.index(of: languageToSearch) ?? "Not found")
票数 0
EN

Stack Overflow用户

发布于 2018-06-04 04:04:45

正如其他人已经说过的,我不明白使用字典数组作为表视图的模型的意义……然而,如果我理解你的问题,这会有帮助吗?问候

var testDictArray: [Dictionary<String, String>] = [] // Just a test, an array of dictionaries

// Filling the dictionary with example data
testDictArray.append(
    ["A0":"a0",
    "B0":"b0",
    "C0":"c0"]
)
testDictArray.append(
    ["A1":"a1",
     "B1":"b1",
     "C1":"c1",
     "D1":"d1"]
)
testDictArray.append(
    ["A2":"a2",
     "B2":"b2"]
)

// The example values you want to find
let upperCase = "B2"
let lowerCase = "b2"

// Some loops...
var foundedIndex: Int? = nil
for index in 0..<testDictArray.count {
    for (key, value) in testDictArray[index] {
        if (key, value) == (upperCase, lowerCase) {
            foundedIndex = index
            break
        }
    }
    if foundedIndex != nil {
        break
    }
}

// Printing result
if let myIndex = foundedIndex {
    print("The index is \(myIndex)") // The example returns 2
} else {
    print("No index found :(")
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50669809

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档