向基于文档的应用添加开放功能

时间:2012-11-20 14:40:50

标签: objective-c cocoa

我有一个基于文档的应用程序,带有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

谢谢!

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

1 个答案:

答案 0 :(得分:2)

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

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

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