简体   繁体   中英

How to fetch .plist / property list content and show in swift 4.2

如何获取 .plist / 属性列表内容并在 swift 4.2 中显示

// 1. Forming resource URL of property list file

// 2. getting Data from url

// 3.serializing plist data to NSObject type classess .

1.guard let plistUrl = Bundle.main.url(forResource: "Your property- list    file name ", withExtension: "plist"),
2.let plistData = try? Data(contentsOf: plistUrl) else { return nil }
var plistEntries: [[String: Any]]? = nil
do { 
 3. plistEntries = try PropertyListSerialization.propertyList(from: plistData,    options: [], format: nil) as? [[String: Any]]
} catch {
  print("error reading plist")
}



for property in plistEntries {
      guard let property1 = property["key-name"] as? String,
      let property2 = property["key-name"] as? NSNumber
 else { fatalError("Error reading data") }
      print("value: \(property1)")
      print("value: \(property2)")

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM