访问 plist 数组 Swift XCode
Accessing plist array Swift XCode
我有一个具有这种结构的 plist。
Root
item0 Dictionary
village string
bars array
item0 dictionary
name string
item1 dictionary
name string
item2 dictionary
item1 Dictionary
village string
bars array
item0 dictionary
name string
item1 dictionary
name string
item2 dictionary
在我的代码中,我想访问特定村庄的 bars 数组,然后遍历 bar 数组中的所有字典并输出每个字典中的字符串值。
到目前为止,我必须访问 plist 并抓取村庄。
var villages = [AnyObject]()
let filePath = Bundle.main.path(forResource: "Bars", ofType: "plist")
if let path = filePath {
villages = NSArray(contentsOfFile: path) as! [AnyObject]
}
从这里我将如何访问我的 villages 数组以获取该 villages 数组中特定索引处的条(数组)?
那么我如何从那里访问 bars 字典[索引].字符串值?
let bars = villages[index]["bars"] as! NSArray //get specific index bars in villiages
let name = (bars[index] as! NSDictionary)["name"] as! String //get specific index name in bars
我有一个具有这种结构的 plist。
Root
item0 Dictionary
village string
bars array
item0 dictionary
name string
item1 dictionary
name string
item2 dictionary
item1 Dictionary
village string
bars array
item0 dictionary
name string
item1 dictionary
name string
item2 dictionary
在我的代码中,我想访问特定村庄的 bars 数组,然后遍历 bar 数组中的所有字典并输出每个字典中的字符串值。
到目前为止,我必须访问 plist 并抓取村庄。
var villages = [AnyObject]()
let filePath = Bundle.main.path(forResource: "Bars", ofType: "plist")
if let path = filePath {
villages = NSArray(contentsOfFile: path) as! [AnyObject]
}
从这里我将如何访问我的 villages 数组以获取该 villages 数组中特定索引处的条(数组)?
那么我如何从那里访问 bars 字典[索引].字符串值?
let bars = villages[index]["bars"] as! NSArray //get specific index bars in villiages
let name = (bars[index] as! NSDictionary)["name"] as! String //get specific index name in bars