我有一个这样的字典:
var dataDictionary : [String:String] = ["Result" : "result", "Date Added" : "dateAdded", "Entered By" : "enteredBy"]
存储这个键和值我已经创建了一个关键字的数组和这样的值:
var selectedDictKeys :[String] = [String]()
var selectedDictValues :[String] = [String]()
并将键和值保存到数组中:
self.selectedDictKeys = Array(self.dataDictionary.keys)
self.selectedDictValues = Array(self.dataDictionary.values)
这是我的defaultName Model类:
class DefaultNameModel: EVObject {
var key : String = ""
var value : String = ""
}
现在我如何打印设置值的值,如..
self.selectedDictKeys = Array(self.dataDictionary.keys)
self.selectedDictValues = Array(self.dataDictionary.values)
var dataSet = [DefaultNameModel]()
for i in 0...(self.selectedDictKeys.count-1)
{
let dataRow = DefaultNameModel()
dataRow.key = String(self.selectedDictKeys)
if dataRow.key == "Result"
{
dataRow.value == "Success"
dataSet.append(dataRow)
print("The data Row is\(dataRow)")
}
if dataRow.key == "Date Added"
{
dataRow.value == "2016-12-12"
dataSet.append(dataRow)
print("The data Row is\(dataRow)")
}
if dataRow.key == "Entered By"
{
dataRow.value == "Me"
dataSet.append(dataRow)
print("The data Row is\(dataRow)")
}
}
答案 0 :(得分:2)
for key in self.selectedDictKeys {
let dataRow = DefaultNameModel()
dataRow.key = key
switch key {
case "Result":
dataRow.value == "Success"
case "Date Added":
dataRow.value == "2016-12-12"
case "Entered By":
dataRow.value == "Me"
default:
dataRow.value == ""
}
dataSet.append(dataRow)
print("The data Row is\(dataRow)")
}
答案 1 :(得分:1)
var dataSet = [DefaultNameModel]()
for i in self.selectedDictKeys
{
let dataRow = DefaultNameModel()
dataRow.key = i
if dataRow.key == "Result"
{
dataRow.value == "Success"
dataSet.append(dataRow)
print("The data Row is " + dataRow.Key + " " + dataRow.value)
}
if dataRow.key == "Date Added"
{
dataRow.value == "2016-12-12"
dataSet.append(dataRow)
print("The data Row is " + dataRow.Key + " " + dataRow.value)
}
if dataRow.key == "Entered By"
{
dataRow.value == "Me"
dataSet.append(dataRow)
print("The data Row is " + dataRow.Key + " " + dataRow.value)
}
}
答案 2 :(得分:1)
我看到你正在使用EVReflection(EVObject是你的基本类型)。然后,您可以基于字典启动对象。为什么不把数据的值放在字典中呢?更好的是,为什么不用常规属性而不是键和值属性创建对象。以下是一些有效的示例代码:
func testxx() {
// Why not setting the values in the dictionary?
let dataDictionary: NSDictionary = ["Result" : "Success", "Date Added" : "2016-12-12", "Entered By" : "Me"]
// Then you could just map it to the model using:
let defaultNameModel: [DefaultNameModel] = dataDictionary.map { DefaultNameModel(dictionary: ["key": $0.key, "value": $0.value]) }
print("defaultNameModelOld = \(defaultNameModel)")
// But you could directly map it to a real model
let defaultNameModelNew = DefaultNameModelNew(dictionary: dataDictionary)
print("defaultNameModelNEw = \(defaultNameModelNew)")
}
class DefaultNameModel: EVObject {
var key: String?
var value: String?
}
class DefaultNameModelNew: EVObject {
var Result: String?
var Date_Added: String?
var Entered_By: String?
}