子类NSTextStorage导致重大内存问题

时间:2016-06-21 19:11:02

标签: swift uitextview textkit nstextstorage

我有一个自定义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

    }

}

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()
    }

 }

1 个答案:

答案 0 :(得分:4)

哇....很奇怪,当我将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()
    }

 }