1

我有一个 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")

现在我想阅读表中的用户默认值

    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 而不是 files[indexPath.row]我是否更正了 nsuserdefault 中的保存以及如何阅读这部分中的 userdefault,这意味着我想阅读一个 PDF 文档

4

0 回答 0