简体   繁体   中英

Swift Delegate in own class

I am currently trying to write my first Swift Mac application. Currently I have hard times refactoring some code into another class.

Current Status:

import Cocoa

class TestClass: NSObject, NSTextStorageDelegate {

    @IBOutlet var codeTextView: NSTextView!
    var syntaxParser:TRexSyntaxKitParser?
    var textStorage : NSTextStorage!
    init(syntaxParser:TRexSyntaxKitParser, textView:NSTextView) {
        self.syntaxParser = syntaxParser

        super.init()

        if let textViewStorage = textView.textStorage {
            self.textStorage = textViewStorage
            self.textStorage.delegate = self
        }
    }

    func textStorageDidProcessEditing(notification: NSNotification) {
        let inputString = self.textStorage.string
        let wholeRange = NSMakeRange(0, count(inputString))

        self.textStorage.removeAttribute(NSForegroundColorAttributeName, range:wholeRange)

        let attributes = self.syntaxParser!.parse(inputString)
        print("Attributes: \(attributes)")

        for attribDict: [String:AnyObject] in attributes {

            let range = NSMakeRange(attribDict["rangeStart"] as! Int, attribDict["rangeLength"] as! Int)
            self.textStorage.addAttribute(attribDict["attributeKey"] as! String, value:NSColor(hexString: attribDict["color"] as! String)!, range:range)
        }
    }
}

and this is how i call this class:

import Cocoa

class CodeEditorViewController: NSViewController {

    @IBOutlet var codeTextView: NSTextView!
    var syntaxParser:TRexSyntaxKitParser?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.syntaxParser = TRexSyntaxKitParser(language:"latex",theme:"classic")
        let testClass = TestClass(syntaxParser: self.syntaxParser!, textView: self.codeTextView)
        codeTextView.lnv_setUpLineNumberView()
    }

but this produces the following error:

[NSFont textStorageDidProcessEditing:]: unrecognized selector sent to instance 

I do not see where I would call the delegate method from NSFont ? So to be precise: How can I refactor the first class into two different one ?

Think about the memory management of this line:

let testClass = TestClass(syntaxParser: self.syntaxParser!, textView: self.codeTextView)

testClass is a local variable . So what happens to your brand new TestClass instance? It comes into existence and immediately vanishes in a puff of smoke when viewDidLoad comes to an end.

Thus, you now have a delegate pointing at an object that does not exist. Hence, the crash.

Solution: make testClass something that will persist long enough to do you some good - like, an instance property of your view controller. That will give you exactly the refactoring you are after (this is a standard design pattern).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM