繁体   English   中英

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

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

我正在尝试从plist获取信息。 这是结构:

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

编辑:和它的一些示例:

<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>
...

所以基本上我是从嵌套在另一个字典中的字典中获取字符串值的。

诀窍是,我想使用tableView

到目前为止,我在这里:

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

let rootRubrique = NSDictionary(contentsOfFile: pathRoot!)

我正在尝试获取这样的信息:

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

但也许问题出在我尝试筛选的方式上

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

我也尝试获得其他值,只是尝试。 这为我工作:

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

我得到了一个小写的“介绍”,第一个。 但是我不想要那个值,我想要嵌套的值! 我快疯了! 对我来说幸运的是Stackoverflow存在!

之后,我将必须找到一种方法以正确的顺序对字典进行排序,这似乎是另一个大问题……

编辑:我解决了这样的第一个问题。 我已经创建了这个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)!
}

}

然后我将索引分配给一个常量:

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

然后我通过func生成整个valueForKey:

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

我不知道为什么它可以使用该技巧,但是它可以使用,而且还不错,因为我需要在其他地方使用索引。 但是不幸的是,我认为我将不得不使用其他东西,例如“ rank”或“ ID”值来对字典进行排序。 所以整个事情到底是没用的哈哈!

您可以使用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

选项1:使用索引遍历所有对象

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

在您的情况下使用索引路径访问

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

选项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