Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift:比较字典键_Ios_Swift_Nsdictionary - Fatal编程技术网

Ios Swift:比较字典键

Ios Swift:比较字典键,ios,swift,nsdictionary,Ios,Swift,Nsdictionary,我有一本这样的字典: var dataDictionary : [String:String] = ["Result" : "result", "Date Added" : "dateAdded", "Entered By" : "enteredBy"] var selectedDictKeys :[String] = [String]() var selectedDictValues :[String] = [String]() self.selectedDictKeys = Array(

我有一本这样的字典:

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)
为了存储此键和值,我制作了一个数组作为键和值,如下所示:

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)
并将键和值保存到数组中,如下所示:

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模型类:

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)")
    }
}

我看到您正在使用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?
}

如果您知道键和值,那么为什么要创建字典?只需创建
DefaultNameModel
的对象和数组中的对象。我必须在表中添加此对象。我如何才能做到这一点..您能建议我..您的键是固定值是固定的,那么这是什么意思,像这样为所有objectUse循环添加相同的值。对于所选DictKeys{..}中的键,并将该键分配给dataRow。