简体   繁体   English

在带有indexPath.row的tableView中从Dictionary获取值?

[英]get value from Dictionary in a tableView with an indexPath.row?

I'm trying to get info from a plist. 我正在尝试从plist获取信息。 Here's the struct : 这是结构:

Root
-Dictionary
--Dictionary
---String
---String
--Dictionary
---String
---String
--Dictionary
---String
---String

edit : And a little sample of it : 编辑:和它的一些示例:

<plist version="1.0">
<dict>
    <key>introduction</key>
    <dict>
        <key>gfx</key>
        <dict>
            <key>titre</key>
            <string>Introduction</string>
            <key>color</key>
            <string></string>
            <key>miniLogo</key>
            <string></string>
            <key>bigLogo</key>
            <string></string>
        </dict>
...

So basically I'm to get the string value from a dictionary nested in another dictionary. 所以基本上我是从嵌套在另一个字典中的字典中获取字符串值的。

The trick is, I want to use a tableView 诀窍是,我想使用tableView

So far I'm here : 到目前为止,我在这里:

let pathRoot = NSBundle.mainBundle().pathForResource("MyPLIST", ofType: "plist")

let rootRubrique = NSDictionary(contentsOfFile: pathRoot!)

I'm trying to get the info like that : 我正在尝试获取这样的信息:

let rubriqueTitre = NSDictionary(contentsOfFile: pathRoot!)?.allKeys[indexPath.row].objectForKey("gfx")?.valueForKey("titre")

But maybe the problem is from the way I'm trying to screen that 但也许问题出在我尝试筛选的方式上

label.text = imageRubrique! as! String
// And also tried :
label.text = "\(imageRubrique!)"

Also I tried to get other values, just to try. 我也尝试获得其他值,只是尝试。 This is working for me : 这为我工作:

let rubriqueIndex = NSDictionary(contentsOfFile: pathRoot!)?.allKeys[indexPath.row]
 label.text = rubriqueIndex as! String

I get the "introduction" with a lower case, the first one. 我得到了一个小写的“介绍”,第一个。 But I don't want that value, I want the nested one ! 但是我不想要那个值,我想要嵌套的值! I'm getting crazy! 我快疯了! Fortunately for me Stackoverflow exist! 对我来说幸运的是Stackoverflow存在!

After that, I'll have to find a way to sort my dictionary in the right order which seems to be another big problem… 之后,我将必须找到一种方法以正确的顺序对字典进行排序,这似乎是另一个大问题……

edit : I solved the first issue like that. 编辑:我解决了这样的第一个问题。 I've created this func : 我已经创建了这个func:

func getValueFromPLIST(rub: String, sousDossier : String, sousSousDossier : String?, value : String) -> String {

if sousSousDossier == nil {

    return (NSDictionary(contentsOfFile: pathRoot!)!.objectForKey(rub)!.objectForKey(sousDossier)!.valueForKey(value) as? String)!
} else {

    return (NSDictionary(contentsOfFile: pathRoot!)!.objectForKey(rub)!.objectForKey(sousDossier)!.objectForKey((sousSousDossier!))!.valueForKey(value) as? String)!
}

}

then I'm assigning the index to a constant : 然后我将索引分配给一个常量:

let rubriqueIndex = NSDictionary(contentsOfFile: pathRoot!)?.allKeys[indexPath.row]

And then I'm generating the whole valueForKey via the func : 然后我通过func生成整个valueForKey:

let rubriquePathTitre = getValueFromPLIST(String(rubriqueIndex!), sousDossier: "gfx", sousSousDossier: nil, value: "titre")

I have no idea why it's working with that trick, but it's working, and it's not so bad since I need the index somewhere else. 我不知道为什么它可以使用该技巧,但是它可以使用,而且还不错,因为我需要在其他地方使用索引。 But unfortunately I think I'm gonna have to use something else, like a "rank" or "ID" value to sort my dictionary. 但是不幸的是,我认为我将不得不使用其他东西,例如“ rank”或“ ID”值来对字典进行排序。 So the whole thing is useless in the end haha! 所以整个事情到底是没用的哈哈!

You can use UITableView index instead iteration index 您可以使用UITableView索引代替迭代索引

// *** Access data from plist ***
let pathRoot = NSBundle.mainBundle().pathForResource("MyPLIST", ofType: "plist")

// *** Get Root element of plist ***
let rootRubrique = NSDictionary(contentsOfFile: pathRoot!)

// *** get Next element which is `Introduction` holding all other objects ***
let objIntro:NSDictionary = rootRubrique!.objectForKey("introduction") as! NSDictionary

Option 1 : Iterate Through all objects using Index 选项1:使用索引遍历所有对象

for var i = 1; i <= objIntro.allValues.count; ++i
{
    print("Index[\(i)] \(objIntro.allValues[0])")
}    

Access Using indexpath in your case 在您的情况下使用索引路径访问

let obj = (objIntro.allValues[indexPath.row])

Option 2 : Iterate through allValues of dictionary 选项2:遍历字典的allValues

for (key, value) in objIntro
{
    print("key: \"\(key as! String)\" obj : \(value)")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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