• 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
Re: Style Question: accessing methods in the Document
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Style Question: accessing methods in the Document


  • Subject: Re: Style Question: accessing methods in the Document
  • From: Bill Bumgarner <email@hidden>
  • Date: Wed, 31 May 2006 17:08:11 -0700

On May 31, 2006, at 4:50 PM, Shawn Seet wrote:
I have a project which is document based and I need my custom views to be able to access some methods in my document object. I am doing this successfully using

[[[[NSApp keyWindow] windowController] document] methodName];

or

[[[[self window] windowController] document] methodName];

A couple of things tell me this is poor practice. I get a warning at compilation that no method "methodName" was found. And this technique fails if there is a parameter involved.
Please excuse the basic nature of this question. I'm new to this. And I've looked through documentation but I feel that this might be something so obvious (to you all) that it isn't documented.

The warning is because -document returns an NSDocument instance and - methodName is [should be] defined on your NSDocument subclass.


You can typecast the -document return value to your subclass:


[(MyDocument *)[[[NSApp keyWindow] windowController] document] methodName];


And the compiler warning should go away.

Instead of using the above, I would suggest...

[[NSDocumentController sharedDocumentController] currentDocument]

... as a good way to get a hold of your current document. The cast will still be required.

This does raise a slightly more fundamental design question. What are you doing that requires obtaining the current document in this fashion? Most of the time, the design of an app is such that control naturally passes through the current document at the appropriate times.

There are certainly situations where using something like - currentDocument is necessary (as is indicated by the fact that it exists at all). As you are new to this, I figured I'd ask.

b.bum
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Style Question: accessing methods in the Document (From: Shawn Seet <email@hidden>)

  • Prev by Date: Style Question: accessing methods in the Document
  • Next by Date: Re: NSAttributedString drawing and NSText drawing
  • Previous by thread: Style Question: accessing methods in the Document
  • Next by thread: [WWDC] Food sucks, complain here...
  • Index(es):
    • Date
    • Thread