Swift 5:如何读取plist文件中的变量?

wmomyfyw  于 2022-12-10  发布在  Swift
关注(0)|答案(3)|浏览(173)

我搜索了一种方法(或模板)如何读取存储在plist文件中的变量。我看到了一些代码,但没有代码与最新的Swift版本一起工作。
我是新来的斯威夫特,我实际上不检查它是如何工作的..谁能这么好,给予我一个例子?
我有一个CollectionView,想在我的plist文件中显示字符串。这是我的plist文件:
对于每个动物键(字典类型)两个子键(名称和图片)。
除了“name”和“picture”以外,所有的键都是字典。现在我想要取得字典,并在我的收藏集检视中显示名称和图像。稍后我会加入更多的变数。我希望可以理解:'D
不完整的代码我有:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    let path = Bundle.main.path(forResource: "Animals", ofType:"plist")!
    let dict = NSDictionary(contentsOfFile: path)
    SortData = dict!.object(forKey: "Animals") as! [Dictionary]
}
6tdlim6h

6tdlim6h1#

您的属性列表格式不是很方便。因为您需要一个数组,所以无论如何都要为键animals创建一个数组的属性列表(并以小写形式命名所有键)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>animals</key>
    <array>
        <dict>
            <key>name</key>
            <string>Tiger</string>
            <key>picture</key>
            <string>tiger_running</string>
        </dict>
        <dict>
            <key>name</key>
            <string>Jaguar</string>
            <key>picture</key>
            <string>jaguar_sleeping</string>
        </dict>
    </array>
</dict>
</plist>

然后创建两个结构体

struct Root : Decodable {
    let animals : [Animal]
}

struct Animal : Decodable {
    let name, picture : String
}

数据源阵列

var animals = [Animal]()

解码这些东西

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    let url = Bundle.main.url(forResource: "Animals", withExtension:"plist")!
    do {
        let data = try Data(contentsOf: url)
        let result = try PropertyListDecoder().decode(Root.self, from: data)
        self.animals = result.animals
    } catch { print(error) }
}

PropertyListDecoderPropertyListSerialization是Swift中最先进的技术。NSDictionary/NSArray API * 客观-c-ish* 并且已经过时。

axr492tv

axr492tv2#

您可以使用PropertyListSerialization或NSDictionary,就像您之前所做的那样。在这里,您很可能是由于其他原因而出错。

class ViewController: UIViewController {

    var animals = [String: Any]()

    override func viewDidLoad() {
        super.viewDidLoad()

        fetchAnimals() // or fetchPropertyListAnimals()
        fetchPropertyListAnimals()
    }

    func fetchAnimals() {

        guard let path = Bundle.main.path(forResource: "Animals", ofType: "plist") else {
            print("Path not found")
            return
        }

        guard let dictionary = NSDictionary(contentsOfFile: path) else {
            print("Unable to get dictionary from path")
            return
        }

        if let animals = dictionary.object(forKey: "Animals") as? [String: Any] {
            self.animals = animals
        } else {
            print("Unable to find value for key named Animals")
        }
    }

    func fetchPropertyListAnimals() {
        var propertyListFormat =  PropertyListSerialization.PropertyListFormat.xml
        guard let path = Bundle.main.path(forResource: "Animals", ofType: "plist") else {
            print("Path not found")
            return
        }
        guard let data = FileManager.default.contents(atPath: path) else {
            print("Unable to get data from path")
            return
        }
        do {
            if let animals = try PropertyListSerialization.propertyList(from: data, options: .mutableContainersAndLeaves, format: &propertyListFormat) as? [String: Any] {
                self.animals = animals
            } else {
                print("Unable to find value for key named Animals")
            }

        } catch {
            print("Error reading plist: \(error), format: \(propertyListFormat)")
        }
    }
}
qzlgjiam

qzlgjiam3#

/*
  //Use: let plistData = Bundle().parsePlist(ofName: "Your-Plist-Name")
   ---------- OR ---------
   guard let path = Bundle.main.path(forResource: "GoogleService-Info", ofType: "plist"),
   let myDict = NSDictionary(contentsOfFile: path) else {
   return nil
  }
 let value = myDict.value(forKey: "CLIENT_ID") as! String?
*/

extension Bundle {
    func parsePlist(ofName name: String) -> [String: AnyObject]? {
    // check if plist data available
    guard let plistURL = Bundle.main.url(forResource: name, withExtension: "plist"),
          let data = try? Data(contentsOf: plistURL)
    else { return nil }
    
    // parse plist into [String: Anyobject]
    guard let plistDictionary = try? PropertyListSerialization.propertyList(from: data, options: [], format: nil) as? [String: AnyObject] else {
        return nil
    }
    return plistDictionary
  }
}

相关问题