Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8868449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:16:27+00:00 2026-06-14T17:16:27+00:00

I have a Document based Application, with an TextView. I want to add an

  • 0

I have a Document based Application, with an TextView.
I want to add an open feature that writes it in the TextView.
I have the code, but it woundn’t work.
The TextView is empty.
Heres my code:

    #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

Thanks!

Please do not flag it as “Not a real Question” or something else.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:16:29+00:00Added an answer on June 14, 2026 at 5:16 pm

    The problem is the readXXX methods are called before the window is created. This means that _textView is nil. You need to use -(void)windowControllerDidLoadNib:(NSWindowController *)windowController to populate _textView with the information you load from the file.

    You can avoid being caught out by this kind of problem in future by placing NSAssert calls in your code to confirm the preconditions required for your methods to operate correctly:

    NSAssert(_textView != nil, @"_textView not initialized");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Cocoa, Document-based Mac OS X application. One feature that I have
I have created a Cocoa document based picture drawing application. I want that the
I have existing java code and need to create Design Document based on that.
I have the following code that creates a submenu based on current document. The
I have a document-based application with a view controller that contains a table, array
I have a document-based application but I am not using undo. How do I
I have a NSDocument based application with a nib that represents the visual document.
I have a document-based application that is designed to process existing documents, not to
I have a non document-based Core Data application. There's an NSTreeController that manages a
For my document-based application I want to have a list of Recent Documents, as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.