问题描述
我有三个 textFields,一个用于键,一个用于键的值,另一个用于我想要的值的 id,如果我选择 ex: "Color" key in key textField 然后显示值 textField : red and white .. .. 当我在值 textField 中选择白色时,然后在 id textField 中显示此值的 id 等等,并使用 pickerView 使用食物键
这是我的回答:
"properties": [
{
"Color": [
{
"id": "2b47d72a-6d9a-4a5c-b1b5-858c85033a46","value": "Red"
},{
"id": "0597b98d-f414-42f1-94cc-b9f9456868d6","value": "White"
}
]
},{
"Food": [
{
"id": "5b6f1094-31aa-4597-8e19-dddb69d20e08","value": "Cheat Meal"
},{
"id": "d670b3c6-6b04-489b-8807-f7c69d85b7a7","value": "Chicken brost"
}
]
}
]
结构
import Foundation
struct ListCategoriesData : Codable {
let statusCode : Int
let message : String
let data : CategoriesListData
enum CodingKeys: String,CodingKey {
case statusCode = "statusCode"
case message = "message"
case data
}
}
import Foundation
struct CategoriesListData : Codable {
let id : String
let rootUserId : Int
let profileId : Int
let name : String
let unit : String
let tags : [String]
let media : String
let createdAt : String
let updatedAt : String
let properties : [[String: [PropertiesValues] ]]
enum CodingKeys: String,CodingKey {
case id = "id"
case rootUserId = "rootUserId"
case profileId = "profileId"
case name = "name"
case unit = "unit"
case tags
case media = "media"
case createdAt = "createdAt"
case updatedAt = "updatedAt"
case properties
}
}
import Foundation
struct PropertiesValues : Codable {
let id : String
let value: String
}
获取密钥请求
class func requestKeysList(completionHandler: @escaping ([String],Error?) -> Void) {
var request = URLRequest(url: EndPoints.getKeys.url)
request.addValue("eyJhbGciKQ",forHTTPHeaderField: "x-auth-token")
let task = URLSession.shared.dataTask(with: request) { (data,response,error) in
guard let data = data else {
completionHandler([],error)
return
}
let decoder = JSONDecoder()
do {
let keysResponse = try decoder.decode(ListCategoriesData.self,from: data)
print(keysResponse)
let keys = keysResponse.data.properties.flatMap({$0.keys})
print(keys)
completionHandler(keys,nil)
} catch {
dispatchQueue.main.async {
completionHandler([],error)
}
}
}
task.resume()
}
取值请求
class func requestValuesList(completionHandler: @escaping ([PropertiesValues],Error?) -> Void) {
var request = URLRequest(url: EndPoints.getKeys.url)
request.addValue("eyJhbGciKQ",forHTTPHeaderField: "x-auth-token")
let task = URLSession.shared.dataTask(with: request) { (data,error)
return
}
let decoder = JSONDecoder()
do {
let keysResponse = try decoder.decode(ListCategoriesData.self,from: data)
print(keysResponse)
let keys = keysResponse.data.properties.flatMap({$0.values.flatMap({$0})})
print(keys)
completionHandler(keys,error)
}
}
}
task.resume()
}
获取值请求的 id
class func requestCategoryIdList(completionHandler: @escaping ([PropertiesValues],error)
return
}
let decoder = JSONDecoder()
do {
let keysResponse = try decoder.decode(ListCategoriesData.self,from: data)
print(keysResponse)
let keys = keysResponse.data.properties.flatMap({$0.values.flatMap({$0})})
print(keys)
completionHandler(keys,error)
}
}
}
task.resume()
}
控制器检索键和值的请求
func getAllKeys() {
apimanager.requestKeysList { (keys:[String],error: Error?) in
// keys var which has [String]
ListKeysModel.keys = keys
print(ListKeysModel.keys)
dispatchQueue.main.async {
self.pickerView.reloadAllComponents()
}
}
}
func getAllValues() {
apimanager.requestValuesList { (values:[PropertiesValues],error: Error?) in
// value var which has [PropertiesValues]
ListKeysModel.values = values
print(ListKeysModel.values)
dispatchQueue.main.async {
self.pickerView.reloadAllComponents()
}
}
}
PickerView 使用
extension ProductProperties :UIPickerViewDelegate,UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView,numberOfRowsInComponent component: Int) -> Int {
switch pickerView.tag {
case 1:
return ListKeysModel.keys.count
case 2:
return ListKeysModel.values.count
case 3:
return ListKeysModel.id.count
default:
return 1
}
}
func pickerView(_ pickerView: UIPickerView,titleForRow row: Int,forComponent component: Int) -> String? {
switch pickerView.tag {
case 1:
return ListKeysModel.keys[row]
case 2:
return "\(ListKeysModel.values[row].value)"
case 3:
return "\(ListKeysModel.id[row].id)"
default:
return "Data Not Fount"
}
}
func pickerView(_ pickerView: UIPickerView,didSelectRow row: Int,inComponent component: Int) {
switch pickerView.tag {
case 1:
propertyTextField.text = ListKeysModel.keys[row]
propertyTextField.resignFirstResponder()
case 2:
valueTextFiedl.text = "\(ListKeysModel.values[row].value)"
valueTextFiedl.resignFirstResponder()
case 3:
idLabel.text = "\(ListKeysModel.id[row].id)"
default:
return
}
}
}
## the problem here there is no validation I got all values even I picked a specific value ex: I got red,white,cheat meal,chicken brost not red and white or cheat meal and chicken brost and the same I got all ids in id textField not only the id of the value I picked I will be so happy if anyone helped me ##
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)