gpt4 book ai didi

objective-c - 为基于文档的应用程序添加开放功能

转载 作者:行者123 更新时间:2023-12-03 17:04:37 25 4
gpt4 key购买 nike

我有一个基于文档的应用程序,带有一个 TextView。我想添加一个开放功能,将其写入 TextView 中。我有代码,但它不起作用。TextView 是空的。这是我的代码:

    #import "Document.h"

@implementation Document

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
NSFont *courier = [NSFont fontWithName: @"Courier" size:12];
[_textView setString: @"Blabla"];
[_textView setFont:courier];
NSLog(@"Tesg");
[_textView setString:@"TEST"];


}

- (id)init
{
NSLog(@"Tesg");

self = [super init];
if (self) {

// Add your subclass-specific initialization here.



}
return self;
}

- (NSString *)windowNibName
{
// Override returning the nib file name of the document
// If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead.
return @"Document";
}

- (void)windowControllerDidLoadNib:(NSWindowController *)aController
{
[super windowControllerDidLoadNib:aController];
// Add any code here that needs to be executed once the windowController has loaded the document's window.
}

+ (BOOL)autosavesInPlace
{
return YES;
}

/*- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
{
// Insert code here to write your document to data of the specified type. If outError != NULL, ensure that you create and set an appropriate error when returning nil.
// You can also choose to override -fileWrapperOfType:error:, -writeToURL:ofType:error:, or -writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead.
NSException *exception = [NSException exceptionWithName:@"UnimplementedMethod" reason:[NSString stringWithFormat:@"%@ is unimplemented", NSStringFromSelector(_cmd)] userInfo:nil];
@throw exception;
return nil;
}
*/
- (NSData *)dataOfType:(NSString *)pTypeName error:(NSError **)pOutError {

NSDictionary * zDict;

if ([pTypeName compare:@"public.plain-text"] == NSOrderedSame ) {
zDict = [NSDictionary dictionaryWithObjectsAndKeys:
NSPlainTextDocumentType,
NSDocumentTypeDocumentAttribute,nil];
} else {
NSLog(@"ERROR: dataOfType pTypeName=%@",pTypeName);
*pOutError = [NSError errorWithDomain:NSOSStatusErrorDomain
code:unimpErr
userInfo:NULL];
return NULL;
} // end if

NSString * zString = [[_textView textStorage] string];
NSData * zData = [zString dataUsingEncoding:NSASCIIStringEncoding];
return zData;

} // end dataOfType
/*
- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
{
// Insert code here to read your document from the given data of the specified type. If outError != NULL, ensure that you create and set an appropriate error when returning NO.
// You can also choose to override -readFromFileWrapper:ofType:error: or -readFromURL:ofType:error: instead.
// If you override either of these, you should also override -isEntireFileLoaded to return NO if the contents are lazily loaded.
NSLog(data);
NSException *exception = [NSException exceptionWithName:@"UnimplementedMethod" reason:[NSString stringWithFormat:@"%@ is unimplemented", NSStringFromSelector(_cmd)] userInfo:nil];
@throw exception;
return YES;
}
*/

- (BOOL)readFromData:(NSData *)pData
ofType:(NSString *)pTypeName
error:(NSError **)pOutError {

if ([pTypeName compare:@"public.plain-text"] != NSOrderedSame) {
NSLog(@"** ERROR ** readFromData pTypeName=%@",pTypeName);
*pOutError = [NSError errorWithDomain:NSOSStatusErrorDomain
code:unimpErr
userInfo:NULL];
return NO;
} // end if

NSDictionary *zDict = [NSDictionary dictionaryWithObjectsAndKeys:
NSPlainTextDocumentType,
NSDocumentTypeDocumentAttribute,
nil];
NSDictionary *zDictDocAttributes;
NSError *zError = nil;
zNSAttributedStringObj =
[[NSAttributedString alloc]initWithData:pData
options:zDict
documentAttributes:&zDictDocAttributes
error:&zError];
if ( zError != NULL ) {
NSLog(@"Error readFromData: %@",[zError localizedDescription]);
return NO;
} // end if
NSString *content = [zNSAttributedStringObj string];
NSLog(@"%@", content);
NSLog(@"%c", [_textView isEditable]);
[_textView setString:content];


return YES;

} // end readFromData


@end

谢谢!

请不要将其标记为“不是真正的问题”或其他内容。

最佳答案

问题是在创建窗口之前调用了 readXXX 方法。这意味着 _textView 为零。您需要使用 -(void)windowControllerDidLoadNib:(NSWindowController *)windowController 使用从文件加载的信息填充 _textView

您可以通过在代码中放置 NSAssert 调用来确认您的方法正确运行所需的先决条件,从而避免将来遇到此类问题:

NSAssert(_textView != nil, @"_textView not initialized");

关于objective-c - 为基于文档的应用程序添加开放功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13475580/

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