当前位置: 动力学知识库 > 问答 > 编程问答 >

ios - get data from combination of dictionary and array from plist in swift 2.2

问题描述:

I hava a plist file which configuration is like this.

Its root is a dictionary which has 3 item, item1,item2,item3

Item 3 itself is an array.

When i get the values of the root_dict, then the item3 returns the value as array.

Item3 has an item (item4) which is a dictionary. Item 4 has an item(item5)

i want to get the value of item5

root_dict

item1 --- string

item2 --- dict

item3 --- array

|

------ item 4 ---- dic

|

-------------item5----array

i am using the below code:

 var myDict: NSDictionary?

if let path = NSBundle.mainBundle().pathForResource("info", ofType: "plist") {

myDict = NSDictionary(contentsOfFile: path) //mydict has 3 item

}

var dicData = myDict!["item3"] as! Dictionary<String, AnyObject>

for (key, value) in dicData{

print(key)

}

its not working.

Any kind of help will be highly appreciated.

网友答案:

I am totally agree with @Nirav Do as follow might be work for you.

    var myDict: NSDictionary?


        if let path = NSBundle.mainBundle().pathForResource("fetchData", ofType: "plist") {
            myDict = NSDictionary(contentsOfFile: path) //mydict has 3 item
        }

        var dicData = myDict!["item3"] as! NSArray
        print(dicData.valueForKey("item5"))           

Your console output should be ( ( //Here is your data ) )

网友答案:

Have you try this

var myDict: NSDictionary?
if let path = NSBundle.mainBundle().pathForResource("info", ofType: "plist") {
    myDict = NSDictionary(contentsOfFile: path) //mydict has 3 item
}
let dicData = myDict!["item3"] as! [[String : AnyObject]] // If this not work try this below statment

//let dicData = myDict!["item3"] as! NSArray

//Now get your dictionary object from array like this

let dic = dicData.firstObject as! [String : AnyObject]
let item5Array = dic["item5"] as! NSArray
print(item5Array) // item5Array is array that you want.     
网友答案:

finally got the answer.But i want to clear the concept first.

1. first, root returns a dict key.One of the dict key is array.
2. so, first from dict, i have to get that array.
3. Again, array contains an dict item. so i have to convert the array to dict.

i use a function, where i pass the key as my required. i can use this function 2 or 3 times or as my wish based on key.

//get the plist and get the root dict
if let path = NSBundle.mainBundle().pathForResource("test", ofType: "plist") 
    {
        myDict = NSDictionary(contentsOfFile: path)
    }

than use the below function in loop 2 or 3 or 4 or more based on dict key.

var array:NSArray? //declare array

func convertArrayToDictFromPlist(keyName:String) {

   var dictArray = [NSDictionary]()

    for (key, value) in myDict!{

        //print(key)
        if(key.isEqual(keyName)){

            array = myDict![keyName] as! NSArray//convert dict key to array

            for i in 0..<array!.count{

                myDict = array![i] as! NSDictionary // if that array contains a dict item, convert and append that dict
                dictArray.append(myDict!)
            }

            print(key)
            break

        }

    }

}
分享给朋友:
您可能感兴趣的文章:
随机阅读: