Swift: 如何在 plist 中读取数组中的多个数组?
Swift: How to read multiple array within array in plist?
这是我的plist
集合视图
使用 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 中,要读取的数组不仅仅是根。我想在我的集合视图中读取根有多少个数组,我在 numberOfItemsInSection
、cellForItemAt
和 didSelectItemAt
中使用它。我还想读取数组中的项目,以便在选择项目时在集合的详细视图中显示它。
结论:我想详细了解根数组中有多少个数组以在 .count
中使用它,以及如何访问数组中的项目以在另一个视图控制器中显示它。要访问一个数组,它将是 array[2]
但如果我想显示字符串 "Jeans" 和 "4" 等,那将如何工作。谢谢
我猜你的数据有误structure.Maybe你可以试试下面的代码...
var mainGroup = [[Any]]()
var myArray: [[Any]]?
if let path = Bundle.main.path(forResource: "items", ofType: "plist") {
myArray = NSArray(contentsOfFile: path)
}
if let myArr = myArray {
mainGroup = myArr
}
struct ParsePlist {
let plistName:String
init(name:String) {
plistName = name
}
func sectionArrayFromPlist() -> [[Any]]? {
guard let plistPath = Bundle.main.path(forResource: plistName, ofType: "plist") else {
return nil
}
guard let rootArray = NSArray(contentsOfFile: plistPath) as? [Any] else {
return nil
}
var sectionArray:[[Any]]?
sectionArray = rootArray.flatMap({ [=10=] as? [Any]})
return sectionArray
}
}
使用:
let parsePlist = ParsePlist(name: <yourplistname>)
if let sectionArray = parsePlist.sectionArrayFromPlist() {
// use sectionArray
}
我已经分别对 rootArray & subArray 添加了注释
override func viewDidLoad() {
super.viewDidLoad()
readPlist()
}
func readPlist(){
let path = Bundle.main.path(forResource: "SampleData", ofType: "plist")
let rootArray = NSArray(contentsOfFile: path!)!
print(rootArray.count)//Gives count for total objects. You can use in number of rows
for data in rootArray {
let subArray = data as? NSArray ?? []
print(subArray.count)//Gives you subarray count
for value in subArray {
print("objects are \(value)")//Gives you contains of subarray
}
}
}
}
Swift 中推荐的读取 属性 列表文件的方法是 PropertyListSerialization
。它避免了 Foundation NSArray
.
的使用
该代码打印节号和内部数组中的所有项目。
如果 属性 列表应该填充 table 视图,我建议将内部数组声明为字典。
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)
}
这是我的plist
集合视图
使用 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 中,要读取的数组不仅仅是根。我想在我的集合视图中读取根有多少个数组,我在 numberOfItemsInSection
、cellForItemAt
和 didSelectItemAt
中使用它。我还想读取数组中的项目,以便在选择项目时在集合的详细视图中显示它。
结论:我想详细了解根数组中有多少个数组以在 .count
中使用它,以及如何访问数组中的项目以在另一个视图控制器中显示它。要访问一个数组,它将是 array[2]
但如果我想显示字符串 "Jeans" 和 "4" 等,那将如何工作。谢谢
我猜你的数据有误structure.Maybe你可以试试下面的代码...
var mainGroup = [[Any]]()
var myArray: [[Any]]?
if let path = Bundle.main.path(forResource: "items", ofType: "plist") {
myArray = NSArray(contentsOfFile: path)
}
if let myArr = myArray {
mainGroup = myArr
}
struct ParsePlist {
let plistName:String
init(name:String) {
plistName = name
}
func sectionArrayFromPlist() -> [[Any]]? {
guard let plistPath = Bundle.main.path(forResource: plistName, ofType: "plist") else {
return nil
}
guard let rootArray = NSArray(contentsOfFile: plistPath) as? [Any] else {
return nil
}
var sectionArray:[[Any]]?
sectionArray = rootArray.flatMap({ [=10=] as? [Any]})
return sectionArray
}
}
使用:
let parsePlist = ParsePlist(name: <yourplistname>)
if let sectionArray = parsePlist.sectionArrayFromPlist() {
// use sectionArray
}
我已经分别对 rootArray & subArray 添加了注释
override func viewDidLoad() {
super.viewDidLoad()
readPlist()
}
func readPlist(){
let path = Bundle.main.path(forResource: "SampleData", ofType: "plist")
let rootArray = NSArray(contentsOfFile: path!)!
print(rootArray.count)//Gives count for total objects. You can use in number of rows
for data in rootArray {
let subArray = data as? NSArray ?? []
print(subArray.count)//Gives you subarray count
for value in subArray {
print("objects are \(value)")//Gives you contains of subarray
}
}
}
}
Swift 中推荐的读取 属性 列表文件的方法是 PropertyListSerialization
。它避免了 Foundation NSArray
.
该代码打印节号和内部数组中的所有项目。
如果 属性 列表应该填充 table 视图,我建议将内部数组声明为字典。
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)
}