使用 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
我得到小写的 "introduction",第一个。但我不想要那个值,我想要嵌套的值!我快疯了!对我来说幸运的是 Whosebug 存在!
在那之后,我必须想办法把我的字典按正确的顺序排序,这似乎是另一个大问题……
编辑:我就这样解决了第一个问题。我创建了这个函数:
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:遍历字典的所有值
for (key, value) in objIntro
{
print("key: \"\(key as! String)\" obj : \(value)")
}
我正在尝试从 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
我得到小写的 "introduction",第一个。但我不想要那个值,我想要嵌套的值!我快疯了!对我来说幸运的是 Whosebug 存在!
在那之后,我必须想办法把我的字典按正确的顺序排序,这似乎是另一个大问题……
编辑:我就这样解决了第一个问题。我创建了这个函数:
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:遍历字典的所有值
for (key, value) in objIntro
{
print("key: \"\(key as! String)\" obj : \(value)")
}