使用NSUserDefaults保存并阅读PDFDocument

时间:2017-10-04 10:27:06

标签: ios swift nsuserdefaults pdfkit userdefaults

我有一个PDFDocument

var documents = [PDFDocument]()

我想保存在userdefaults

在这个功能中

override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {

let movedObject = self.documents[sourceIndexPath.row]
    documents.remove(at: sourceIndexPath.row)
    documents.insert(movedObject, at: destinationIndexPath.row)

    NSLog("%@", "\(sourceIndexPath.row) => \(destinationIndexPath.row) \(documents)")


    var index = 0
    let documentDic = NSMutableDictionary()
    for element in documents
    {
        documentDic.setValue(index, forKey: (element.documentURL?.absoluteString)!)
        index = index + 1
    }


    UserDefaults.standard.set(documentDic, forKey: "ReorderingFormat")

现在我想阅读表

中的userdefaults
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! BookshelfCell

         let document = documents[indexPath.row]
    if let documentAttributes = document.documentAttributes {
        if let title = documentAttributes["Title"] as? String {
            cell.title = title
        }
        if let author = documentAttributes["Author"] as? String {
            cell.author = author
        }
        if document.pageCount > 0 {
            if let page = document.page(at: 0), let key = document.documentURL as NSURL? {
                cell.url = key

                if let thumbnail = thumbnailCache.object(forKey: key) {
                    cell.thumbnail = thumbnail
                } else {
                    downloadQueue.async {
                        let thumbnail = page.thumbnail(of: CGSize(width: 40, height: 60), for: .cropBox)
                        self.thumbnailCache.setObject(thumbnail, forKey: key)
                        if cell.url == key {
                            DispatchQueue.main.async {
                                cell.thumbnail = thumbnail
                            }
                        }
                    }
                }
            }
        }
    }
    return cell
}

并添加 userdefault而不是文档[indexPath.row] 我更正了nsuserdefault中的保存,如何读取此部分中的用户默认,这意味着我想在一个PDF文档中阅读

0 个答案:

没有答案