你如何在不知道字典名称的情况下在字典中查找内容
How do you find something in a dictionary without knowing the name of the dictionary
我正在使用 Swift 游乐场“Anwsers 模板”
假设我有:
设苹果=[“成本”:10,“营养”:5]
让香蕉= [“成本”:15,“营养”:10]
让 choice = askForChoice(Options:[“Apple”, “Banana”])
有什么好的、简单的方法可以找到每种水果的成本,而不使用“if”函数,因为我可能会制作 100 多种不同的东西。
// A good, more object oriented way-
struct Fruit{
var name: String
var cost: Double
var nutrition: Int
}
let fruitsDataHolder = [
Fruit(name: "Apple", cost: 10.0, nutrition: 5),
Fruit(name: "Banana", cost: 15.0, nutrition: 10)
]
func getFruitsCost(fruits: [Fruit]) -> Double{
var totalCost = 0.0
for fruit in fruits{ totalCost += fruit.cost }
return totalCost
}
print(getFruitsCost(fruits: fruitsDataHolder)) // prints 25.0
如果你坚持用字典来做:
let fruitsDataHolder2 = [
["name": "Apple", "cost": 10.0, "nutrition": 5],
["name": "Banana", "cost": 15.0, "nutrition": 10]
]
func getFruitsCost2(fruits: [[String: Any]]) -> Double{
var totalCost = 0.0
for fruit in fruits{
let cost = fruit["cost"] as! Double
totalCost += cost
}
return totalCost
}
print(getFruitsCost2(fruits: fruitsDataHolder2)) // prints 25.0
编辑
这是根据他的名字获得特定水果成本的方法
对于第一种方式-
func getFruitCost(fruitName: String, fruits: [Fruit]) -> Double?{
// searching for the fruit
for fruit in fruits{
if fruit.name == fruitName{
// found the fruit, returning his cost
return fruit.cost
}
}
// couldn't find that fruit
return nil
}
对于第二种方式-
func getFruitCost2(fruitName: String, fruits: [[String: Any]]) -> Double?{
// searching for the fruit
for fruit in fruits{
let currentFruitName = fruit["name"] as! String
if currentFruitName == fruitName{
// found the fruit, returning his cost
return fruit["cost"] as! Double
}
}
// couldn't find that fruit
return nil
}
我正在使用 Swift 游乐场“Anwsers 模板” 假设我有:
设苹果=[“成本”:10,“营养”:5] 让香蕉= [“成本”:15,“营养”:10]
让 choice = askForChoice(Options:[“Apple”, “Banana”])
有什么好的、简单的方法可以找到每种水果的成本,而不使用“if”函数,因为我可能会制作 100 多种不同的东西。
// A good, more object oriented way-
struct Fruit{
var name: String
var cost: Double
var nutrition: Int
}
let fruitsDataHolder = [
Fruit(name: "Apple", cost: 10.0, nutrition: 5),
Fruit(name: "Banana", cost: 15.0, nutrition: 10)
]
func getFruitsCost(fruits: [Fruit]) -> Double{
var totalCost = 0.0
for fruit in fruits{ totalCost += fruit.cost }
return totalCost
}
print(getFruitsCost(fruits: fruitsDataHolder)) // prints 25.0
如果你坚持用字典来做:
let fruitsDataHolder2 = [
["name": "Apple", "cost": 10.0, "nutrition": 5],
["name": "Banana", "cost": 15.0, "nutrition": 10]
]
func getFruitsCost2(fruits: [[String: Any]]) -> Double{
var totalCost = 0.0
for fruit in fruits{
let cost = fruit["cost"] as! Double
totalCost += cost
}
return totalCost
}
print(getFruitsCost2(fruits: fruitsDataHolder2)) // prints 25.0
编辑 这是根据他的名字获得特定水果成本的方法
对于第一种方式-
func getFruitCost(fruitName: String, fruits: [Fruit]) -> Double?{
// searching for the fruit
for fruit in fruits{
if fruit.name == fruitName{
// found the fruit, returning his cost
return fruit.cost
}
}
// couldn't find that fruit
return nil
}
对于第二种方式-
func getFruitCost2(fruitName: String, fruits: [[String: Any]]) -> Double?{
// searching for the fruit
for fruit in fruits{
let currentFruitName = fruit["name"] as! String
if currentFruitName == fruitName{
// found the fruit, returning his cost
return fruit["cost"] as! Double
}
}
// couldn't find that fruit
return nil
}