• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Need sample code for NSDocument / NSDocumentController
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Need sample code for NSDocument / NSDocumentController


  • Subject: Need sample code for NSDocument / NSDocumentController
  • From: Keith Knauber <email@hidden>
  • Date: Thu, 05 Jul 2012 20:15:32 +0000
  • Thread-topic: Need sample code for NSDocument / NSDocumentController

Please provide sample code for how to migrate from the deprecated
- (id)openDocumentWithContentsOfURL:(NSURL *)absoluteURL display:(BOOL)displayDocument error:(NSError **)outError
to the new approved
-[NSDocumentController openDocumentWithContentsOfURL:display:completionHandler:]


In our case, there can only be one document open at a time.
However, NSDocument is useful for save, revert, edit, NSUndoManager, etc.

The Cocoa docs state that NSDocumentController should rarely need to be subclassed.
However, in Lion, I'm at a loss for why its complaining to me:

-[NSDocumentController openDocumentWithContentsOfURL:display:completionHandler:] failed during state restoration. Here's the error:
"The document “test.mboxshow” could not be opened. MyApp cannot open files in the “"MyApp.app Document” format.

Why is there no standard sample project for a standard NSDocumentController/NSDocument based app?
There is no sample code for the new -[NSDocumentController openDocumentWithContentsOfURL:display:completionHandler:]


Apple developers need to understand that writing a sample app must be part of
publishing any new API, otherwise they must expect developers to misuse their
class.

My attempts to override the new function instead does bad things… like no new windows can be drawn.
My app seems to randomly do bad things, similar to what happens when there is an uncaught exception,
although I see no exceptions being thrown.
Cocoa spits out no log messages, and I have exhausted every conceivable debug method (NSException catch, NSZombie, MallocDebug, etc etc)


For what its worth, my now deprecated NSDocumentController  override never gets called,
whether I override the new method or not.
I'm open to suggestions for an alternate method of achieving this:
- (id)openDocumentWithContentsOfURL:(NSURL *)absoluteURL display:(BOOL)displayDocument error:(NSError **)outError
{
   if ( outError )
      *outError = nil;



   NSLog( @"openDocumentWithContentsOfURL %@", absoluteURL);
   NSString *path = [absoluteURL path];
   if ( [absoluteURL isFileURL] && [[path pathExtension] isEqualToString: kMbEConsoleFileExtension] )
   {
      MbDocument *currShow = [self mbdocument];
      NSString *currShowPath = [currShow filePath];
      NSInteger retVal;
      if ( !( [[MbEController instance] isFinishedLaunching] & kFinishedPreLaunchCheck) )
      {
         // user double-clicked a file to start Director
         currShowPath = [[NSUserDefaults standardUserDefaults] objectForKey:@"LastSaveFile"];
         if ( [currShowPath isEqualToString: path] )
         {
            if ( [[[NSUserDefaults standardUserDefaults] objectForKey:@"isDocumentEdited"] boolValue] == YES )
            {
               retVal = NSRunAlertPanel(@"Open Show File", @"Revert to Saved?", @"Revert to Saved", @"Keep Changes", NULL);
               if ( retVal == NSAlertDefaultReturn )
               {
                  [[MbEController instance] openShowFile:path];
               }
            }
            return currShow; // proceed with normal startup
         }
      }



      if ( [currShowPath isEqualToString: path] )
      {
         NSLog( @"is edited %d currShow %@", [currShow isDocumentEdited], currShow );
         if ( [currShow isDocumentEdited] )
            [currShow revertDocumentToSaved:self]; // will prompt
         else // document is already open, so do what Apple's standard action is...
            [currShow showWindows];
      }
      else
      {
         if ( [currShow isDocumentEdited] )
            retVal = NSRunAlertPanel(@"Open Show File", [NSString stringWithFormat: @"The current show file has unsaved changes.  Discard unsaved changes and switch to show file '%@'?", path], @"Discard unsaved changes and switch to show", @"Keep Current", NULL);
         else
            retVal = NSRunAlertPanel(@"Switch to Show File", [NSString stringWithFormat: @"Switch to Show File '%@'?\n\nCurrent show file '%@'", path, currShowPath ? currShowPath : @"Untitled"], @"Switch", @"Keep Current", NULL);
         if ( retVal == NSAlertDefaultReturn )
            [[MbEController instance] openShowFile:path];
      }

      // user cancelled
      return currShow;
   }
   return [super openDocumentWithContentsOfURL:absoluteURL display:displayDocument error:outError];
}
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden


  • Follow-Ups:
    • Fwd: Need sample code for NSDocument / NSDocumentController
      • From: Keith Knauber <email@hidden>
  • Prev by Date: Creating an easily reusable NSViewController
  • Next by Date: Help: Create, import and edit SVG object in Cocoa app
  • Previous by thread: Creating an easily reusable NSViewController
  • Next by thread: Fwd: Need sample code for NSDocument / NSDocumentController
  • Index(es):
    • Date
    • Thread