在Swift中从Dictionary中提取值

时间:2022-06-26 13:21:41

Aim: If the input in text1 matches with a value in superDictionary,then a second dictionary (that corresponds with that value which matched) has it's values multiplied by the number value in num1 WHEN button1 is pressed (a mouthful I know)

目的:如果text1中的输入与superDictionary中的值匹配,那么第二个字典(与匹配的那个值相对应)将其值乘以num1中的数字值按下按钮1(我知道一口)

e.g. if text1.text = "apple" (which matches with a value in superDictionary) (see in code below), then that match redirects me to appleDictionary whose every entry then becomes multiplied by the number value in num1 (say 5) when button1 is pressed.

例如if text1.text =“apple”(与superDictionary中的值匹配)(参见下面的代码),然后该匹配将我重定向到appleDictionary,当button1为button1时,其每个条目将乘以num1中的数字值(比如5)按下。

The problem: I don't know what syntax to use to do this!

问题:我不知道用什么语法来做这个!

My IBOutlets and IBActions are as follows:

我的IBOutlets和IBActions如下:

@IBOutlet weak var text1: UITextField!

@IBOutlet weak var num1: UITextField!

var superDictionary = ["apple","banana"]

var appleDictionary = ["fibre": 1, "water": 2]

@IBAction func button1(sender: AnyObject) {}

Thanks in advance

提前致谢

1 个解决方案

#1


1  

I would suggest that you combine your dictionaries and make a true superDictionary:

我建议你结合你的词典并制作一个真正的超级字典:

var superDictionary = ["apple": ["fibre": 1, "water": 2], "banana": ["potassium": 2, "water": 2]]

Then your button action would be something like this:

然后你的按钮动作将是​​这样的:

@IBAction func button1(sender: AnyObject) {
    guard let num = Int(num1.text ?? "") else { return }
    guard let text = text1.text else { return }
    guard var dict = superDictionary[text] else { return }

    for (key, value) in dict {
        dict[key] = value * num
    }

    superDictionary[text] = dict
}

Notes:

  • The first guard statement extracts num from the num1 textField. If the textField is nil the nil coalescing operator ?? turns it into an empty String which Int makes into nil. If there is a string that can successfully be converted to an Int, the guard statement succeeds and assigns it to num, otherwise the function returns.
  • 第一个guard语句从num1 textField中提取num。如果textField为零nil合并运算符??把它变成一个空的String,Int将其变成nil。如果存在可以成功转换为Int的字符串,则guard语句成功并将其分配给num,否则函数返回。

  • The second guard statement assigns the string in text1.text to text or returns if the textField is nil.
  • 第二个guard语句将text1.text中的字符串赋值给text或如果textField为nil则返回。

  • The third guard statement assigns the proper dictionary to dict if it exists, or returns if there is no such dictionary. This guard uses var instead of let because we will be modifying this dictionary.
  • 第三个保护语句将正确的字典分配给dict是否存在,或者如果没有这样的字典则返回。这个守卫使用var代替let,因为我们将修改这个字典。

  • The loop modifies the dictionary by extracting key/value pairs and by replacing the value with value * num.
  • 循环通过提取键/值对并通过将值替换为值* num来修改字典。

  • The final assignment replaces the dictionary in the superDictionary with the modified one.
  • 最后的赋值用superDictionary替换了修改后的字典。

#1


1  

I would suggest that you combine your dictionaries and make a true superDictionary:

我建议你结合你的词典并制作一个真正的超级字典:

var superDictionary = ["apple": ["fibre": 1, "water": 2], "banana": ["potassium": 2, "water": 2]]

Then your button action would be something like this:

然后你的按钮动作将是​​这样的:

@IBAction func button1(sender: AnyObject) {
    guard let num = Int(num1.text ?? "") else { return }
    guard let text = text1.text else { return }
    guard var dict = superDictionary[text] else { return }

    for (key, value) in dict {
        dict[key] = value * num
    }

    superDictionary[text] = dict
}

Notes:

  • The first guard statement extracts num from the num1 textField. If the textField is nil the nil coalescing operator ?? turns it into an empty String which Int makes into nil. If there is a string that can successfully be converted to an Int, the guard statement succeeds and assigns it to num, otherwise the function returns.
  • 第一个guard语句从num1 textField中提取num。如果textField为零nil合并运算符??把它变成一个空的String,Int将其变成nil。如果存在可以成功转换为Int的字符串,则guard语句成功并将其分配给num,否则函数返回。

  • The second guard statement assigns the string in text1.text to text or returns if the textField is nil.
  • 第二个guard语句将text1.text中的字符串赋值给text或如果textField为nil则返回。

  • The third guard statement assigns the proper dictionary to dict if it exists, or returns if there is no such dictionary. This guard uses var instead of let because we will be modifying this dictionary.
  • 第三个保护语句将正确的字典分配给dict是否存在,或者如果没有这样的字典则返回。这个守卫使用var代替let,因为我们将修改这个字典。

  • The loop modifies the dictionary by extracting key/value pairs and by replacing the value with value * num.
  • 循环通过提取键/值对并通过将值替换为值* num来修改字典。

  • The final assignment replaces the dictionary in the superDictionary with the modified one.
  • 最后的赋值用superDictionary替换了修改后的字典。