gpt4 book ai didi

json - 从 Swift 中选定的下拉选项中获取数据

转载 作者:行者123 更新时间:2023-11-28 15:29:45 26 4
gpt4 key购买 nike

我从 JSON 重发中得到的是:

 "meal" : {
"id" : 354,
"unit" : "cup",
"disable" : "0",
"created_at" : "2017-04-17 13:09:12",
"small" : "0",
"type" : "7",
"price" : "0",
"image" : "da6ddbab1f7b8b37a536c353fac16e3958f4be78022f6.png",
"family_id" : "125",
"updated_at" : "2017-04-17 13:32:41",
"deleted_at" : null,
"medium" : "10",
"large" : "30",
"name" : "Grapefruit"

例如,这种果汁有“中号”和“大号”两种尺寸。在下拉列表中,我希望只显示有价格的尺码,只有尺码,当用户选择尺码时,我希望打印它的价格和所选尺码。所以我找到了这个库,我一直在试图找出一种方法来成功实现我想要的但没有运气,因为它只会向我显示没有项目大小的选定项目(或没有价格大小的选定价格)

 @IBAction func placeDropdown(_ sender: Any) {

Size.show()

}

func sizeShow() {
Size.anchorView = Size
Size.bottomOffset = CGPoint(x: 0, y: sizebtn.bounds.height)
// You can also use localizationKeysDataSource instead. Check the docs.
let prices = [sSize , mSize , lSize]
print(prices)
var newPrices = [String]()
print(newPrices)
for i in prices {

if i != "0"{

newPrices.append(i)
}
}

Size.dataSource = newPrices
// Action triggered on selection
Size.selectionAction = { [unowned self] (index, item) in
self.sizebtn.setTitle(item, for: .normal)

print(self.Size.selectedItem)
}

}

有什么方法或图书馆可以帮助我吗?

最佳答案

Vyacheslav 是对的,您的 json 就此而言很奇怪,尤其是当不同的餐点可以有不同类型的大小时。尝试像这样构建您的 json:

“meal”:{
// ...
“sizes”: [{“type”:”medium”, “price”: 10}, {“type”:”large”, “price”: 30}]
}

这样你就可以像这样得到不同大小的数组:

availableSizes = (mealJSON[“sizes”] as? [[String: AnyObject]]) ?? []

尽管您的 Size 变量(我假设是您的下拉 UI)必须调整其输入。如果您只想要大于 0 的价格,请添加:

availableSizes = availableSizes.filter { ($0[“price”] as? Int) ?? 0 > 0 }

选择尺寸后,希望您的函数将获得用户选择作为参数的尺寸数组的索引,以便您可以通过以下方式访问所选尺寸:

availableSizes[selectedIndex][“type”]

关于json - 从 Swift 中选定的下拉选项中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44853888/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com