简体   繁体   中英

Swift:Comparing Dictionary Keys

i have a dictionary like this:

var dataDictionary : [String:String] = ["Result" : "result", "Date Added" : "dateAdded", "Entered By" : "enteredBy"]

to store this key and values i have made a array foe a key and values like this:

var selectedDictKeys :[String] = [String]()
var selectedDictValues :[String] = [String]()

and kept the keys and values into array like this:

self.selectedDictKeys = Array(self.dataDictionary.keys)
self.selectedDictValues = Array(self.dataDictionary.values)

This is my defaultName Model class:

class DefaultNameModel: EVObject {
    var key : String = ""
    var value : String = ""
}

Now how can i print set the value for values like..

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

            }
        }

I see that you are using EVReflection (EVObject is your base type). Then you could initiate your objects based on a dictionary. Why don't you put the values for the data in the dictionary? And even better, why don't you create an object with regular properties instead of a key and a value property. Here is some sample code that works:

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?
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM