搜索Swift中价值词典数组 [英] Search Array of Dictionaries for Value in Swift

查看:113
本文介绍了搜索Swift中价值词典数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始Swift并且上课学习iOS编程。我发现自己陷入了如何搜索字典数组的字典数组,并将字符串值转储到数组中。这是取自我的Xcode操场。



我试图找出如何:
1)通过一系列字典搜索
2 )将搜索结果转储成数组(我创建的)



这些是字符字典。

  let worf = [
name:Worf,
rank:中尉,
信息:儿子莫贡,杀人者Gowron,
最喜爱的饮料:修剪汁,
报价:今天是一个美好的一天死亡]

let picard = [
name:Jean-Luc Picard,
rank:队长,
信息:USS企业队长,
最喜欢的饮料:茶,伯爵灰色,热]

这是一个字符数组

  let characters = [worf,picard] 

这是我要写的功能。

  func favoriteDrinksArrayFo rCharacters(characters:Array< Dictionary< String,String>) - >阵列<字符串> {
//创建一个字符串数组,以转换为喜欢的饮料字符串
var favoriteDrinkArray = [String]()

用于字符字符{
// look最喜欢的饮料

//添加喜爱的饮料到收藏夹DbinkArray
}

return favoriteDrinkArray
}

let favoriteDrinks = favoriteDrinksArrayForCharacters (字符)

收藏夹收藏夹

我将不胜感激,向前迈进。我已经挖了一些例子,但我刚刚找到一个适用于我在这里做的事情。

解决方案

在循环中,您需要从字典中获取最喜爱的饮料条目,并将其附加到数组:

 以字符为单位的字符{
if let drink = character [favorite drink] {
favoriteDrinkArray.append(drink)
}
}

注意如果喝酒= 防止有可能没有数组中的这样的条目 - 如果没有,你会得到一个 nil ,而如果正在检查只有添加条目,如果它不是零。



有时候,有人会看到人们跳过,如果让而是写让饮料=字符[最喜爱的饮料]!,最后有一个感叹号。 不要这样做。这被称为强制解开一个可选项,如果从字典返回有效值,您的程序将崩溃。



第一个行为例如,如果没有饮料,你不要将它添加到数组。但是这可能不是你想要的,因为你可能期望字符数组中的条目和饮料数组中的条目之间的1对1的对应。



你可能想要一个空字符串,你可以这样做:

  func favoriteDrinksArrayForCharacters(characters:[[String: String]]) - > [String] {
return characters.map {character in
character [favorite drink] ??
}
}

.map 意味着:运行字符中的每个条目,并将运行此表达式的结果放在一个新数组中(然后返回) p>

?? 意味着:如果你回来了一个 nil 从左侧,将其替换为右侧的值。


I'm new to Swift and taking a class to learn iOS programming. I find myself stumped on how to search in an array of dictionaries for a string value and dump the string value into an array. This is taken from my Xcode playground.

I'm trying to figure out how to: 1) search through an array of dictionaries 2) dump the results of the search into an array (which I've created)

These are the character dictionaries.

let worf = [
    "name": "Worf",
    "rank": "lieutenant",
    "information": "son of Mogh, slayer of Gowron",
    "favorite drink": "prune juice",
    "quote" : "Today is a good day to die."]

let picard = [
    "name": "Jean-Luc Picard",
    "rank": "captain",
    "information": "Captain of the USS Enterprise",
    "favorite drink": "tea, Earl Grey, hot"]

This is an array of the character dictionaries listed above.

let characters = [worf, picard]

This is the function I'm trying to write.

func favoriteDrinksArrayForCharacters(characters:Array<Dictionary<String, String>>) -> Array<String> {
    // create an array of Strings to dump in favorite drink strings
    var favoriteDrinkArray = [String]()

    for character in characters {
        // look up favorite drink

        // add favorite drink to favoriteDrinkArray
    }

    return favoriteDrinkArray
}

let favoriteDrinks = favoriteDrinksArrayForCharacters(characters)

favoriteDrinks

I would be grateful for any assistance on how to move forward on this. I've dug around for examples, but I'm coming up short finding one that's applicable to what I'm trying to do here.

解决方案

Inside the loop, you need to fetch the "favorite drink" entry from the dictionary, and append it to the array:

for character in characters {
    if let drink = character["favorite drink"] {
        favoriteDrinkArray.append(drink)
    }
}

Note, the if let drink = guards against the possibility there is no such entry in the array – if there isn't, you get a nil back, and the if is checking for that, only adding the entry if it's not nil.

You might sometimes see people skip the if let part and instead just write let drink = character["favorite drink"]!, with an exclamation mark on the end. Do not do this. This is known as "force unwrapping" an optional, and if there is ever not a valid value returned from the dictionary, your program will crash.

The behavior with the first example is, if there is no drink you don't add it to the array. But this might not be what you want since you may be expecting a 1-to-1 correspondence between entries in the character array and entries in the drinks array.

If that's the case, and you perhaps want an empty string, you could do this instead:

func favoriteDrinksArrayForCharacters(characters: [[String:String]]) -> [String] {
    return characters.map { character in
        character["favorite drink"] ?? ""
    }
}

The .map means: run through every entry in characters, and put the result of running this expression in a new array (which you then return).

The ?? means: if you get back a nil from the left-hand side, replace it with the value on the right-hand side.

这篇关于搜索Swift中价值词典数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆