【问题标题】:Use URL to Load Plist File into Array使用 URL 将 Plist 文件加载到数组中
【发布时间】:2017-03-02 15:54:13
【问题描述】:

我创建了一个在表格视图中包含员工目录的应用。该应用程序在我的项目中使用 plist 文件来创建字典数组时运行良好,但文件经常随着员工的来来去去而变化,所以我真的需要将它放在 Xcode 项目的外部。我正在尝试使用 URL 和 NSURL 来做到这一点,但是在让它工作时遇到了问题,需要重新审视。假设文件位于 www.abc.com/directory.plist,完成的最佳方法是什么?
对不起,我几乎是一个编程初学者,但我正在尽可能快地学习!

这是我的功能代码:

class TableViewController: UITableViewController {

var filePath: String?
var employees: [[String: String]] = []

override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.delegate = self
    self.tableView.dataSource = self

    filePath = Bundle.main.path(forResource: "directory", ofType: "plist")
    employees = NSArray(contentsOfFile: filePath!) as! [[String: String]]

    for item in employees {

        print(item["Name"] as Any)
        print(item.count)
        print(employees.count)
    }
}

编辑 -
我在代码中用 PropertyListSerialization 替换了 NSArray。仍在使用 URLSession 添加远程文件加载。

        if let filePath = Bundle.main.url(forResource: "directory", withExtension: "plist") {
        do {
            let data = try Data(contentsOf:filePath)
            employees = try PropertyListSerialization.propertyList(from: data, options: [], format: nil) as! [[String : String]]

        } catch {
            print("Error loading file.")
        }
    }

【问题讨论】:

  • 您需要URLSessiondataTask(with:completion)PropertyListSerialization。在 SO 上有数千个示例如何从远程 URL 加载数据。
  • NSURLSession 应该可以解决问题。然后在 (NS)Data 对象检索上调用 propertyList(from data:, options, format:) 以创建您的数组。然后,拨打reloadData()
  • 正如我所说,我是一个初学者,我并没有轻易发布这个问题,而是在几个星期的过程中一直如此,并尝试了几十个例子。我会再玩一些 URLSession/NSURLSession。感谢您的反对。

标签: ios swift3 xcode8 nsurl


【解决方案1】:

我让它工作了。感谢您使用 URLSession 为我指明正确的方向。我阅读了 Apple 的文档,然后拼凑了 Xcode 自动完成功能和几个示例,其中一些来自 vadian。我希望这对其他人有帮助。

    override func viewDidLoad() {
    super.viewDidLoad()

    let filePath = URL(string: "https://www.example.com/directory.plist")
    URLSession.shared.dataTask(with: filePath!) { (data, response, error) in
        if error != nil {
            print("Error loading URL.")
        }
    }
    do {
            let data = try Data(contentsOf:filePath!)
            employees = try PropertyListSerialization.propertyList(from: data, options: [], format: nil) as! [[String : String]]

            for item in employees {
                print(item["Name"] as Any)
                print(item.count)
                print(employees.count)
            }
    } catch {
           print("Error loading employee data.")
      }
  }

【讨论】:

    【解决方案2】:

    正如其他人所说,您需要使用NSURLSession(Swift 3 中的URLSession

    这是从远程服务器进行异步下载的现代方式。

    我编写了一个名为 Async_demo 的小演示项目,您可以从 Github 下载它,其中包含一个类 DownloadManager。您可以将该类放入您的项目中,以将您的 plist 数据下载为 Data 对象。然后,您将该数据保存到应用的文档或缓存目录中。

    【讨论】:

      【解决方案3】:

      URLSession.shared.dataTask(with: filePath!) { (data, response, error) 和 PropertyListSerialization.propertyList(from: data, options: [] 绝对是要走的路。很好地弄清楚并发布你的在这里为社区的其他人回答。

      【讨论】:

        猜你喜欢
        • 2012-07-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多