Ios 为什么PDFKit.beginFindStrings的结果为零?
自学新手在这里 我的最终目标: iOS/Mac应用程序,加载PDF目录,搜索每个PDF的字符串数组,并列出哪些PDF包含哪些字符串 仅对一个PDF进行原型设计时出现问题: 加载选定的PDF,运行Ios 为什么PDFKit.beginFindStrings的结果为零?,ios,swift,pdfkit,Ios,Swift,Pdfkit,自学新手在这里 我的最终目标: iOS/Mac应用程序,加载PDF目录,搜索每个PDF的字符串数组,并列出哪些PDF包含哪些字符串 仅对一个PDF进行原型设计时出现问题: 加载选定的PDF,运行.beginFindStrings([“and”,“the”],带有选项:。不区分大小写)并等待通知.pdfdocumentdidendfindfind检查[PDFSelection],我收到了一个令人困惑的零 那不应该。内存显示PDF已加载。我的线程有问题吗?我想我已经遵循了这里的async建议: 代码
.beginFindStrings([“and”,“the”],带有选项:。不区分大小写)
并等待通知.pdfdocumentdidendfindfind
检查[PDFSelection]
,我收到了一个令人困惑的零
那不应该。内存显示PDF已加载。我的线程有问题吗?我想我已经遵循了这里的async
建议:
代码
import UIKit
import MobileCoreServices
import PDFKit
class ViewController: UIViewController, UIDocumentPickerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
var matchesFound: PDFSelection?
@IBOutlet weak var resultsLabel: UILabel!
@IBAction func importPDF(_ sender: Any) {
let picker = UIDocumentPickerViewController(documentTypes: [kUTTypePDF as String], in: .import)
picker.delegate = self
picker.allowsMultipleSelection = false
self.present(picker, animated: true)
}
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
guard urls.count == 1 else {return}
let data = try! Data(contentsOf: urls[0])
let subjectPDF = PDFDocument.init(data: data)
guard subjectPDF!.isLocked == false else {return}
subjectPDF!.beginFindStrings(["the", "and"], withOptions: .caseInsensitive)
NotificationCenter.default.addObserver(self, selector: #selector(onDidFindMatch(_:)), name: Notification.Name.PDFDocumentDidEndFind, object: nil)
}
@objc func onDidFindMatch(_ notification: Notification) {
resultsLabel.text = "\(String(describing: matchesFound?.string))"
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
dismiss(animated: true, completion: nil)
}
}
带标记的代码
import UIKit
import MobileCoreServices
import PDFKit
class ViewController: UIViewController, UIDocumentPickerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
//Array of PDFSelection search results
var matchesFound: PDFSelection?
//Temporary display for search result strings
@IBOutlet weak var resultsLabel: UILabel!
//Choose a PDF to import, temporarily limited to one
@IBAction func importPDF(_ sender: Any) {
let picker = UIDocumentPickerViewController(documentTypes: [kUTTypePDF as String], in: .import)
picker.delegate = self
picker.allowsMultipleSelection = false
self.present(picker, animated: true)
}
//Load the picked PDF as subjectPDF, if unlocked
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
guard urls.count == 1 else {return}
let data = try! Data(contentsOf: urls[0])
let subjectPDF = PDFDocument.init(data: data)
guard subjectPDF!.isLocked == false else {return}
//Find temporary array of strings
subjectPDF!.beginFindStrings(["the", "and"], withOptions: .caseInsensitive)
//Trigger results readout upon search competion
NotificationCenter.default.addObserver(self, selector: #selector(onDidFindMatch(_:)), name: Notification.Name.PDFDocumentDidEndFind, object: nil)
}
//Readout found strings to temporary label
@objc func onDidFindMatch(_ notification: Notification) {
resultsLabel.text = "\(String(describing: matchesFound?.string))"
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
dismiss(animated: true, completion: nil)
}
}
这个问题是7个月前提出的,我希望你已经找到了解决办法。 无论如何,您的问题的解决方案是: 1-将下面的行移动到
viewDidLoad()
,因为您是在触发beginFindString()
方法后添加观察者的
NotificationCenter.default.addObserver(self, selector: #selector(onDidFindMatch(_:)), name: Notification.Name.PDFDocumentDidEndFind, object: nil)
2-您从未为matchesFound
变量赋值,因此它始终为零
3-要从beginFindString
方法获取匹配项,您需要为PDFDocumentDidFindMatch
添加一个观察者,并从userInfo
而不是PDFDocumentDidEndFind
获取数据。
PDFDocumentDidEndFind
observer将在搜索完成时被调用,例如,您可以使用此observer删除加载视图
以下是正确实现的示例代码:
var matchesFound = [PDFSelection]()
// MARK: Life cycle
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(didFindMatch(_:)), name: NSNotification.Name.PDFDocumentDidFindMatch, object: nil)
}
deinit {
NotificationCenter.default.removeObserver(self)
}
// This method will get called every-time a match has been found.
@objc private func didFindMatch(_ sender: Notification) {
guard let selection = sender.userInfo?["PDFDocumentFoundSelection"] as? PDFSelection else { return }
self.matchesFound.append(selection)
}
谢谢你回到一个老问题上来。我最终只是将它转换成文本,这对于我查询彼此接近的字符串来说更容易。