मैं स्विफ्ट वर्किंग में iOS के लिए एक समृद्ध टेक्स्ट एडिटर पाने की कोशिश कर रहा हूं। मुझे हालांकि निम्नलिखित त्रुटि मिलती है:
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
कोड के इस टुकड़े को चलाते समय:
public var text: String? {
didSet {
guard let text = text else { return }
if editorView.isLoading {
textToLoad = text
} else {
editorView.evaluateJavaScript("richeditor.insertText("(text.htmlEscapeQuotes)");", completionHandler: nil)
placeholderLabel.isHidden = !text.htmlToPlainText.isEmpty
}
}
}
त्रुटि लाइन पर दिखाई देती है
if editorView.isLoading {
.
यही कारण है कि मुझे लगता है कि EditorView "टी आर इनिशियलाइज़्ड नहीं है। हालांकि इसे इनिशिएट किया जाना चाहिए जैसा कि आप इनिट विधि में देख सकते हैं:
private var editorView: WKWebView!
public override init(frame: CGRect = .zero) {
placeholderLabel.textColor = UIColor.lightGray.withAlphaComponent(0.65)
guard let bundlePath = Bundle(for: type(of: self)).path(forResource: "Resources", ofType: "bundle"),
let bundle = Bundle(path: bundlePath),
let scriptPath = bundle.path(forResource: "RichTextEditor", ofType: "js"),
let scriptContent = try? String(contentsOfFile: scriptPath, encoding: String.Encoding.utf8),
let htmlPath = bundle.path(forResource: "RichTextEditor", ofType: "html"),
let html = try? String(contentsOfFile: htmlPath, encoding: String.Encoding.utf8)
else { fatalError("Unable to find javscript/html for text editor") }
let configuration = WKWebViewConfiguration()
configuration.userContentController.addUserScript(
WKUserScript(source: scriptContent,
injectionTime: .atDocumentEnd,
forMainFrameOnly: true
)
)
editorView = WKWebView(frame: .zero, configuration: configuration)
super.init(frame: frame)
[RichTextEditor.textDidChange, RichTextEditor.heightDidChange].forEach {
configuration.userContentController.add(WeakScriptMessageHandler(delegate: self), name: $0)
}
editorView.navigationDelegate = self
...
editorView.scrollView.delegate = self
addSubview(placeholderLabel)
...
addSubview(editorView)
editorView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([...])
editorView.loadHTMLString(html, baseURL: nil)
}
क्या इनिट विधि को नहीं कहा जाता है? या मैं कुछ देख रहा हूँ?
जिस वर्ग में यह सब होता है, उसमें निम्नलिखित हस्ताक्षर हैं:
public class RichTextEditor: UIView, WKScriptMessageHandler, WKNavigationDelegate, UIScrollViewDelegate {
मैं कैसे इस मुद्दे को ठीक कर सकते हैं पर कोई इनपुट स्वागत है! धन्यवाद।
उत्तर:
जवाब के लिए 0 № 1प्रकट होता है कि मेरी फ़ाइल में दो आरंक्षक थे और स्टोरीबोर्ड के साथ उपयोग के लिए कोड नहीं लिखा गया था। सिर्फ init?(coder:)
स्टोरीबोर्ड का उपयोग करते समय बुलाया गया था, इसलिए यही कारण है कि EditorView ने "टी इनिशियलाइज़्ड" नहीं किया है। मैंने अभी से सभी कोड स्थानांतरित कर दिए हैं:
override init(frame: CGRect)
के आरंभिक
required init?(coder decoder: NSCoder)
शुरुआती और यह मेरे लिए मुद्दा तय किया।