Swift:如何在plist中读取数组中的多个数组?

时间:2023-02-06 10:56:01

This is my plist

这是我的plist

Collections View

集合视图

Swift:如何在plist中读取数组中的多个数组?

With Swift 3, I'm reading the plist to fill my arrays with this code perfectly but I do not know how to access the plist when it has arrays inside the root array

使用Swift 3,我正在读取plist来完美地填充我的数组,但是当它在根数组中包含数组时,我不知道如何访问plist

The code I use to read when the root is an array

当根是数组时我用来读取的代码

var mainGroup = [String]()

var myArray: [String]?
if let path = Bundle.main.path(forResource: "items", ofType: "plist") {
    myArray = NSArray(contentsOfFile: path) as? [String]
}

mainGroup = myArray!

But in this plist, theres more arrays to read than just the root. I would like to read how many arrays the root has so in my collection view I use it in numberOfItemsInSection, cellForItemAt and didSelectItemAt. And I would like to also read items inside the arrays to display it in detail view of the collections for when an item is selected.

但是在这个plist中,可以读取的数组比根数组还要多。我想读一下根有多少个数组,所以在我的集合视图中,我在numberOfItemsInSection、cellForItemAt和didSelectItemAt中使用它。我还想读取数组中的项,以便在选择项时显示集合的详细视图。

Conclusion: I want to read how many arrays are in root array to use it in .count and how to access the items in the arrays to display it in another view controller as detail. To access an array it would be array[2] but how would that work if I wanted to display the string "Jeans" and "4" , etc. Thank you

结论:我想要读取根数组中有多少个数组可以在.count中使用,以及如何访问数组中的项以在另一个视图控制器中显示。要访问一个数组,它将是数组[2],但是如果我想显示字符串"Jeans"和"4"等等,那会怎样?谢谢

4 个解决方案

#1


5  

The recommended way in Swift to read a property list file is PropertyListSerialization. It avoids the usage of Foundation NSArray.

Swift中读取属性列表文件的推荐方式是PropertyListSerialization。它避免了基础NSArray的使用。

The code prints the section numbers and all items in the inner array.

代码打印出内部数组中的节号和所有项。

If the property list is is supposed to populate a table view I recommend to declare the inner array as dictionary.

如果属性列表要填充一个表视图,我建议将内部数组声明为dictionary。

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)
}

#2


1  

I have added comment respectively with rootArray & subArray Swift:如何在plist中读取数组中的多个数组?

我分别用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
            }
        }

    }
}

#3


0  

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({ $0 as? [Any]})
        return sectionArray
    }
}

use:

使用:

let parsePlist = ParsePlist(name: <yourplistname>)
if let sectionArray = parsePlist.sectionArrayFromPlist() {
   // use sectionArray
}

#4


-1  

I guess that you make a mistake in data structure.Maybe you can try the code below...

我猜你在数据结构上犯了一个错误。也许你可以试试下面的代码…

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
}

#1


5  

The recommended way in Swift to read a property list file is PropertyListSerialization. It avoids the usage of Foundation NSArray.

Swift中读取属性列表文件的推荐方式是PropertyListSerialization。它避免了基础NSArray的使用。

The code prints the section numbers and all items in the inner array.

代码打印出内部数组中的节号和所有项。

If the property list is is supposed to populate a table view I recommend to declare the inner array as dictionary.

如果属性列表要填充一个表视图,我建议将内部数组声明为dictionary。

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)
}

#2


1  

I have added comment respectively with rootArray & subArray Swift:如何在plist中读取数组中的多个数组?

我分别用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
            }
        }

    }
}

#3


0  

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({ $0 as? [Any]})
        return sectionArray
    }
}

use:

使用:

let parsePlist = ParsePlist(name: <yourplistname>)
if let sectionArray = parsePlist.sectionArrayFromPlist() {
   // use sectionArray
}

#4


-1  

I guess that you make a mistake in data structure.Maybe you can try the code below...

我猜你在数据结构上犯了一个错误。也许你可以试试下面的代码…

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
}