子类化NSTextStorage导致严重的内存问题

Noo*_*ath 4 uitextview nstextstorage textkit swift

我有一个自定义项UITextView,它通过定义一个自定义NSTextStorage类来利用Apple的TextKit ,但是,当我将我的子类用于自定义文本视图的文本存储时(如下所述),并尝试打开任何大于20.0KB的文件,该应用将因崩溃而崩溃到内存泄漏:“ Message from debugger: Terminated due to memory issue”。

奇怪的是,如果我BMTextStorage只用一个标准的替换我的自定义NSTextStorage,则文本立即加载而没有任何内存泄漏,并且使用了小于30MB的RAM。是什么原因造成的?

TextView.swift

class TextView : UITextView {

    required init(frame: CGRect) {

        // If I replace the following line with simply 
        // "let textStorage = NSTextStorage()"
        // I can open any file of any size and not have a memory leak
        // issue, using only about 20-30MB of RAM. If I ran this code
        // as is, it can open most files less than 20KB but will 
        // crash otherwise.
        let textStorage = BMTextStorage() 

        let layoutManager = NSLayoutManager()

        layoutManager.allowsNonContiguousLayout = true

        let textContainer = NSTextContainer(size: CGSizeMake(.max, .max))

        textContainer.widthTracksTextView = true
        textContainer.heightTracksTextView = true
        textContainer.exclusionPaths = [UIBezierPath(rect: CGRectMake(0.0, 0.0, 40.0, .max))]

        layoutManager.addTextContainer(textContainer)
        textStorage.addLayoutManager(layoutManager)

        super.init(frame: frame, textContainer: textContainer)

        textStorage.delegate = self
        layoutManager.delegate = self

    }

}
Run Code Online (Sandbox Code Playgroud)

BMTextStorage.swift

typealias PropertyList = [String : AnyObject]

class BMTextStorage : NSTextStorage {

    override var string: String {
        return storage.string
    }

    private var storage = NSMutableAttributedString()

    override func attributesAtIndex(location: Int, effectiveRange range: NSRangePointer) -> PropertyList {
        return storage.attributesAtIndex(location, effectiveRange: range)
    }

    override func replaceCharactersInRange(range: NSRange, withString str: String) {
        storage.replaceCharactersInRange(range, withString: str)
        edited([.EditedAttributes, .EditedCharacters], range: range, changeInLength: str.length - range.length)
    }

    override func setAttributes(attrs: PropertyList?, range: NSRange) {
        storage.setAttributes(attrs, range: range)
        edited([.EditedAttributes], range: range, changeInLength: 0)
    }

    override func processEditing() {
        super.processEditing()
    }

 }
Run Code Online (Sandbox Code Playgroud)

Noo*_*ath 5

哇....很奇怪,当我将类型更改storage为NSTextStorage... 时,它变得固定了。

typealias PropertyList = [String : AnyObject]

class BMTextStorage : NSTextStorage {

    overrride var string: String {
        return storage.string
    }

    private var storage = NSTextStorage()

    override func attributesAtIndex(location: Int, effectiveRange range: NSRangePointer) -> PropertyList {
        return storage.attributesAtIndex(location, effectiveRange: range)
    }

    override func replaceCharactersInRange(range: NSRange, withString str: String) {
        storage.replaceCharactersInRange(range, withString: str)
        edited([.EditedAttributes, .EditedCharacters], range: range, changeInLength: str.length - range.length)
    }

    override func setAttributes(attrs: PropertyList?, range: NSRange) {
        storage.setAttributes(attrs, range: range)
        edited([.EditedAttributes], range: range, changeInLength: 0)
    }

    override func processEditing() {
        super.processEditing()
    }

 }
Run Code Online (Sandbox Code Playgroud)

  • 我不禁觉得这是来自 Apple API 的大量代码气味......此外,为什么 /heck/ NSTextStorage 是一个“抽象”类,而这在 Objective-C 中毫无意义,这是另一个很好的迹象,表明存在这个强大的班级背后有一些黑魔法。谢谢你的提示,不过!+1 (2认同)