K. Darcy Otto wrote:

@interface MyDocument : NSDocument
{
BOOL sheetOpen; // sheetOpen must be accessed directly; there are no accessors
... other stuff ...
}

I hate to give the bad news, but sheetOpen is an instance variable of MyDocument, not a class variable or static variable.


The lines marked ** surprise me; I had expected they would be zero as well. If I replace these NSLogs with requests to display self, I get (again, my "**"):

self at validateUserInterfaceItem: <MyDocument: 0x1047180>
self before NSApp beginSheet: <MyDocument: 0x1047180>
self at -windowWillBeginSheet: <MyDocument: 0x1047180>
self after NSApp beginSheet: <MyDocument: 0x1047180>
self at validateUserInterfaceItem: <MyDocument: 0x102d430> **
self at -windowDidEndSheet: <MyDocument: 0x1047180>
self at validateUserInterfaceItem: <MyDocument: 0x1047180>

You should log both the value of self and the value of sheetOpen at each log point.


Bizarreness at **!  I mean, -init is only run once!

How do you know -init is only run once?

Are you sure no other init method is run?  Like maybe initWithCoder:?

Look at the "**" in the above log output, and notice there are two distinct addresses being shown, and the clearly have different state. Therefore, there must be two separate and distinct instances of MyDocument. The evidence seems plain, and I see no other interpretation given the posted source code.

You may not understand how two instances of MyDocument could be created and used, but all the evidence supports the conclusion that this is exactly what's happening.

Furthermore, there is nothing at all puzzling about sheetOpen, since it's an instance variable, not a class variable or static variable. Again, you may not understand this, but that doesn't change the fact of it.

Before attempting to debug this, you must first fully understand what your @interface is saying. You don't yet have to understand how or why there are two instances; indeed, that would only be the expected outcome after debugging. But it is absolutely essential that you understand why sheetOpen is an instance variable, not a class or static variable. To do that, I suggest reviewing the basics of Objective-C @interface syntax.

Simple example:
  http://en.wikipedia.org/wiki/Objective-C#Interface

Once you have that covered, you can determine how two instances are being created. I suggest using the debugger to set a breakpoint at MyDocument -init and -initWithCoder, then printing a stack-trace to see how it got there. There are other approaches, but if you don't know how to use the debugger, you need to learn it.

Once the two-instances problem is solved, and you need a way to define an actual class variable, you should start by googling the keywords:

  objective-C class variable

  -- GG

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to