Objective c Cocoa是真正的慢NSTextView。是否有更好的方法将文本附加到NSTextView的内容中?
我对可可非常陌生,这可能是一个全新的问题。然而,我沮丧至死 我有一个非常简单的Cocoa应用程序(称为“lines”),用于测试向文本视图发送1000行文本 我从Xcode4开始使用“new Cocoa project”和所有默认设置。这提供了一个空白窗口对象,我可以在其上拖动IB UI元素 然后,我构建的UI由一个文本视图和NIB文件窗口上的一个按钮组成。我使用Xcode 4将这两个元素拖到.h文件中。文本视图连接到Objective c Cocoa是真正的慢NSTextView。是否有更好的方法将文本附加到NSTextView的内容中?,objective-c,cocoa,macos,Objective C,Cocoa,Macos,我对可可非常陌生,这可能是一个全新的问题。然而,我沮丧至死 我有一个非常简单的Cocoa应用程序(称为“lines”),用于测试向文本视图发送1000行文本 我从Xcode4开始使用“new Cocoa project”和所有默认设置。这提供了一个空白窗口对象,我可以在其上拖动IB UI元素 然后,我构建的UI由一个文本视图和NIB文件窗口上的一个按钮组成。我使用Xcode 4将这两个元素拖到.h文件中。文本视图连接到outView,而“1000”按钮连接到one\u按钮方法 单击按钮“100
outView
,而“1000”按钮连接到one\u按钮
方法
单击按钮“1000”会触发一个循环,将1000行文本(“第1行”、“第2行”、“第1000行”)打印到名为“outView”的NSTextView
以下是完整的代码(除了所描述的.XIB文件):
linesAppDelegate.h:
#import <Cocoa/Cocoa.h>
@interface linesAppDelegate : NSObject <NSApplicationDelegate> {
@private
NSWindow *window;
NSTextView *outView;
}
@property (assign) IBOutlet NSWindow *window;
@property (assign) IBOutlet NSTextView *outView;
- (IBAction)one_thousand_button:(id)sender;
@end
关于源代码、我的原始代码和:
第一次运行增加1000行;第二次运行添加了1000行,以此类推(Adam,base)是他的代码,没有beginEditing
和endEditing
显然,使用开始编辑
和结束编辑
要快得多
请参阅上的苹果文档 试试这个:
- (IBAction)one_thousand_button:(id)sender {
for(int i=1; i<=1000; i++){
[[[outView textStorage] mutableString] appendString:
[NSString stringWithFormat: @"Line %i\n",i]];
}
}
-(iAction)一千按钮:(id)发送者{
对于(int i=1;i在调用beginEditing
和endEditing
时包装对文本存储的更新。这会导致Cocoa保留其所有更改通知,直到您完成对文本存储的更改
- (IBAction)oneThousandButton:(id)sender
{
NSTextStorage *textStorage = [outView textStorage];
[textStorage beginEditing];
for (int i = 1; i <= 1000; i++)
{
NSString *line = [NSString stringWithFormat: @"Line %i\n", i];
[textStorage replaceCharactersInRange:NSMakeRange([textStorage length], 0)
withString:line];
}
[textStorage endEditing];
}
-(iAction)一小时按钮:(id)发送方
{
NSTextStorage*textStorage=[outView textStorage];
[文本存储开始编辑];
对于(int i=1;i),慢度可能不是NSTextView
本身,而是每次循环时分配2个新字符串(1个通过stringWithFormat:
分配,另一个通过stringByAppendingString:
分配)然后在每次迭代中更改文本视图的字符串。@drewk的回答应该能帮助加快速度。“旋转彩色死亡披萨!”如果你像@Adam Preble方法一样添加beginEditing/endEditing
,它的速度可能会更快,更容易阅读…巨大的速度-奇妙的答案-我几乎可以立即加载100000行Lorem Ipsum属性字符串-我得到10000个旋转的海滩球,但没有这个。保险丝熔断,100万行失败。。。
Adam Preble (Adam, base) drewk my pitiful code
1st run: 3ms 269ms 260ms 1,950ms
2nd run 3ms 269ms 250ms 2,332ms
3rd run: 2ms 270ms 260ms 2,880ms
- (IBAction)one_thousand_button:(id)sender {
for(int i=1; i<=1000; i++){
[[[outView textStorage] mutableString] appendString:
[NSString stringWithFormat: @"Line %i\n",i]];
}
}
- (IBAction)oneThousandButton:(id)sender
{
NSTextStorage *textStorage = [outView textStorage];
[textStorage beginEditing];
for (int i = 1; i <= 1000; i++)
{
NSString *line = [NSString stringWithFormat: @"Line %i\n", i];
[textStorage replaceCharactersInRange:NSMakeRange([textStorage length], 0)
withString:line];
}
[textStorage endEditing];
}