【问题标题】:get data from combination of dictionary and array from plist in swift 2.2从swift 2.2中的plist中的字典和数组的组合中获取数据
【发布时间】:2016-11-14 14:10:03
【问题描述】:

我有一个 plist 文件,配置是这样的。

它的根是dictionary,它有3个项目,项目1,项目2,项目3

第 3 项本身是一个array

当我得到 root_dict 的值时,item3 将值返回为array

Item3 有一个项目 (item4),它是 dictionary。 item 4 有一个 item(item5)

我想得到item5的值

root_dict 
    item1 --- string
    item2 --- dict
    item3 --- array
        |
        ------ item 4 ---- dic
                 |
                 -------------item5----array

我正在使用以下代码:

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

它不工作。

我们将不胜感激任何形式的帮助。

【问题讨论】:

  • 您正在尝试将数组转换为字典,这是不可能的,请更改您的代码var dicData = myDict!["item3"] as! [[String:AnyObject]]
  • 如果我使用你的代码,它会给出无法将类型“__NSArrayM”(0x7fff764d25c8)的值转换为“NSDictionary”(0x7fff764d2aa0)。
  • 看起来你的 plist 根对象是一个数组

标签: ios swift macos swift2


【解决方案1】:

我完全同意@Nirav 这样做可能对你有用。

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

你的控制台输出应该是 ( ( //这是你的数据 ) )

【讨论】:

  • 实际上它返回完整的项目 4,但我只想要项目 5 的值。如果项目 4 有许多其他项目。
  • item3 返回数组,其中第 4 项是 dict 项。我如何从返回的数组中获取 item4 dict 值?
【解决方案2】:

终于得到了答案。但我想先弄清楚这个概念。

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.

我使用一个函数,我根据需要传递密钥。我可以使用此功能 2 或 3 次或根据我的意愿使用此功能。

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

比在循环 2 或 3 或 4 或更多基于 dict 键中使用以下函数。

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

        }

    }

}

【讨论】:

    【解决方案3】:

    你试过了吗

    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.     
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多