我正在阅读一些文件内容并尝试使用NSScrollView在NSTextView上显示 . 当我尝试显示非常大的文件的内容时,我的应用程序挂起 . 实际情况是:

  • 打开文件 .

  • 在NSData中读取其内容并将其转换为NSString .

  • 创建NSScrollView .

  • 创建NSTextView .

  • 将上面创建的NSString设置为NSTextView .

文件大小非常大 . 它包含大约106010441行 .

我的应用程序成功地能够显示文件的初始内容,直到我向下滚动它开始挂起并变得非常慢 . 如果我单击文本区域并按命令a选择全部并开始滚动,它会完全冻结 .

我不知道,我做错了什么 .

这是我的ViewController.m内容:

#import "ViewController.h"
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];

    /
    NSString * path = Path_to_file;
    NSFileHandle * fileHandle = [NSFileHandle fileHandleForReadingAtPath:path];
    NSData * buffer = [fileHandle readDataToEndOfFile];
    raw_string = [[NSString alloc] initWithBytes:[buffer bytes] length:[buffer length] encoding:NSUTF8StringEncoding];
}


- (void)setRepresentedObject:(id)representedObject {
    [super setRepresentedObject:representedObject];
}


- (IBAction)button:(id)sender {
    _Scroller.hasVerticalScroller = YES;
    _Scroller.hasHorizontalScroller = YES;
    _Scroller.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;

    NSSize contentSize = _Scroller.contentSize;


    NSTextView* _textView = [[NSTextView alloc] initWithFrame:NSMakeRect(0, 0,
                                                                         contentSize.width, contentSize.height)];

    _textView.minSize = NSMakeSize(0.0, contentSize.height);
    _textView.maxSize = NSMakeSize(FLT_MAX, FLT_MAX);
    _textView.verticallyResizable = YES;
    _textView.horizontallyResizable = YES;
    _textView.autoresizingMask = NSViewWidthSizable;
    _textView.textContainer.containerSize = NSMakeSize(contentSize.width, FLT_MAX);
    _textView.textContainer.widthTracksTextView = YES;
    _textView.automaticSpellingCorrectionEnabled = NO;
    _textView.continuousSpellCheckingEnabled = NO;

    _Scroller.documentView = _textView;
    _textView.string = raw_string;
}
@end