这是我的plist
Collection View
使用 Swift 3,我正在读取 plist 以用这段代码完美地填充我的数组,但我不知道如何在 plist 的根数组中包含数组时访问它
当根是数组时我用来读取的代码
var mainGroup = [String]()
var myArray: [String]?
if let path = Bundle.main.path(forResource: "items", ofType: "plist") {
myArray = NSArray(contentsOfFile: path) as? [String]
}
mainGroup = myArray!
但是在这个 plist 中,要读取的数组不仅仅是根。我想在我的 Collection View 中读取根有多少个数组,我在 numberOfItemsInSection
、cellForItemAt
和 didSelectItemAt
中使用它。我还想读取数组中的项目,以便在选择项目时在集合的详细 View 中显示它。
结论:我想详细了解根数组中有多少个数组以在 .count
中使用它,以及如何访问数组中的项目以在另一个 View Controller 中显示它。要访问一个数组,它将是 array[2]
但如果我想显示字符串 "Jeans"和 "4"等,那将如何工作。谢谢
最佳答案
Swift 中推荐的读取属性列表文件的方法是 PropertyListSerialization
。它避免了使用 Foundation NSArray
。
代码打印内部数组中的节号和所有项目。
如果属性列表应该填充 TableView ,我建议将内部数组声明为字典。
let url = Bundle.main.url(forResource:"items", withExtension: "plist")!
do {
let data = try Data(contentsOf:url)
let sections = try PropertyListSerialization.propertyList(from: data, format: nil) as! [[Any]]
for (index, section) in sections.enumerated() {
print("section ", index)
for item in section {
print(item)
}
}
} catch {
print("This error must never occur", error)
}
关于ios - Swift:如何在 plist 中的数组中读取多个数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46290998/