gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 08:25:36 25 4
gpt4 key购买 nike

我有一个自定义的 UITextView,它通过定义一个自定义的 NSTextStorage 类来利用 Apple 的 TextKit,但是,当我将我的子类用于自定义 TextView 的文本存储时(如下所示)并尝试打开任何大于 20.0KB 的文件,应用程序因内存泄漏而崩溃:“来自调试器的消息:由于内存问题而终止”。

奇怪的是,如果我将我的自定义 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()
}

}

最佳答案

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

}

关于swift - 子类化 NSTextStorage 会导致严重的内存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37952726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com