How to get the application object?

  • Hi folks,

    When creating a new Cocoa project on Mac OS X Beta, PB creates the
    NSApplication object like this in the main function:

          return NSApplicationMain(argc, argv);

    But this give me no chance at all to access the NSApplication object.
    In the Apple documentation it is written that the function
    NSApplicationMain look about like this:

    void NSApplicationMain(int argc, char *argv[]) {
              [NSApplication sharedApplication];
              [NSBundle loadNibNamed:@"myMain" owner:app];
              [NSApp run];
          }

    But where is the returned value for the main function? And what to do
    with the arguments? I want to replace my "return
    NSApplicationMain(argc, argv);" so I can get the NSApplication object
    and set my windows.

    Could you tell me how to access this object?

    Regards.

    Tib.
  • NSApp is a global variable of type (NSApplication *), defined somewhere in the AppKit. This is what you need.

    Raphael
  • > NSApp is a global variable of type (NSApplication *), defined somewhere in the
    > AppKit. This is what you need.

    Also, [NSApplication sharedApplication] can be called again later (in your
    controller / application delegate, for example) to return your NSApplication
    instance.

    >> I want to replace my "return NSApplicationMain(argc, argv);" so I can get the
    >> NSApplication object and set my windows.

    This sounds like you might actually be looking for something else; what are
    you trying to do? If you're trying to do initial setup of your UI and
    windows, that should be done in your controller class(es). Depending on what
    you need to setup, you'll probably want to do it in -awakeFromNib, though a
    few things you might have to wait until -applicationWillFinishLaunching or
    -applicationDidFinishLaunching for.

    --
    Rick Roe
        Webmeister & Icon Dude
        http://www.icons.cx/
  • RTFM:
    http://devworld.apple.com/techpubs/webobjects/WebObjects_3.5/Reference/Fram
    e

    works/ObjC/Foundation/Classes/NSProcessInfo/NSProcessInfo.html

    See the -arguments method.
  • Pick any class in your app.

    In it's init method do

    [NSApp setDelegate:self];

    add the -application(will/should) methods to that class.

    You are done.

    No IB or special equipment required.

    This is in the docs (and I believe it's also in the TextEdit application,
    though i could be wrong).

    Of course, you will most likely wish to choose your main controller object
    as the implementer of these methods, there is no requirement to do so.

    Eric

    On Fri, 26 Jan 2001, Todd Blanchard wrote:

    > <fontfamily><param>Helvetica</param>
    >
    > I certainly understand his dilemma.  I struggled with this just last night. The problem is that there is no good hook that I can find in the api for setting up your application delegate to handle applicationWillFinishLaunching in the first place.
    >
    >
    > Instead, it looks like you have to use IB to create a delegate class and set it up on the application.  If you're not familiar with IB (and I'm not very - its a really unintuitive tool) then its going to take awhile to figure out how to do that.
    >
    >
    > I still haven't quite made it work.
    >
    >
    > On Friday, January 26, 2001, at 01:26 AM, Rick Roe wrote:
    >
    > <color><param>0000,0000,0000</param>
    >
    > <italic></italic></color><italic>Also, [NSApplication sharedApplication] can be called again later (in your</italic><color><param>0000,0000,0000</param>
    >
    > <italic></italic></color><italic>controller / application delegate, for example) to return your NSApplication</italic><color><param>0000,0000,0000</param>
    >
    > <italic></italic></color><italic>instance.</italic><color><param>0000,0000,0000</param>
    >
    >
    > <italic></italic></color><italic>>> I want to replace my "return NSApplicationMain(argc, argv);" so I can get the</italic><color><param>0000,0000,0000</param>
    >
    > <italic></italic></color><italic>>> NSApplication object and set my windows.</italic><color><param>0000,0000,0000</param>
    >
    >
    > <italic></italic></color><italic>This sounds like you might actually be looking for something else; what are</italic><color><param>0000,0000,0000</param>
    >
    > <italic></italic></color><italic>you trying to do? If you're trying to do initial setup of your UI and</italic><color><param>0000,0000,0000</param>
    >
    > <italic></italic></color><italic>windows, that should be done in your controller class(es). Depending on what</italic><color><param>0000,0000,0000</param>
    >
    > <italic></italic></color><italic>you need to setup, you'll probably want to do it in -awakeFromNib, though a</italic><color><param>0000,0000,0000</param>
    >
    > <italic></italic></color><italic>few things you might have to wait until -applicationWillFinishLaunching or</italic><color><param>0000,0000,0000</param>
    >
    > <italic></italic></color><italic>-applicationDidFinishLaunching for.</italic><color><param>0000,0000,0000</param>
    >
    >
    > <italic></italic></color><italic>-- </italic><color><param>0000,0000,0000</param>
    >
    > <italic></italic></color><italic>Rick Roe</italic><color><param>0000,0000,0000</param>
    >
    > <italic></italic></color><italic>    Webmeister & Icon Dude</italic><color><param>0000,0000,0000</param>
    >
    > <italic></italic></color><italic>    http://www.icons.cx/</italic><color><param>0000,0000,0000</param>
    >
    >
    > <italic></italic></color><italic>_______________________________________________</italic><color><param>0000,0000,0000</param>
    >
    > <italic></italic></color><italic>MacOSX-dev mailing list</italic><color><param>0000,0000,0000</param>
    >
    > <italic></italic></color><italic><MacOSX-dev...></italic><color><param>0000,0000,0000</param>
    >
    > <italic></italic></color><italic>http://www.omnigroup.com/mailman/listinfo/macosx-dev</italic><color><param>0000,0000,0000</param>
    >
    > </color>
    >
    > <flushleft><x-tabstops><param>72L;144L;216L;288L;360L;</param>
    >
    > ---<color><param>CBCB,1818,3838</param>
    >
    > Todd Blanchard</color>
    >
    > Enterprise Systems Architect
    >
    > Cacheon, Inc. http://www.cacheon.com
    >
    > (415) 856-1144</x-tabstops></flushleft>_______________________________________________
    > MacOSX-dev mailing list
    > <MacOSX-dev...>
    > http://www.omnigroup.com/mailman/listinfo/macosx-dev
    >

    --
    Eric Peyton
    <epeyton...>

    Software and Source for Mac OS X
  • > I certainly understand his dilemma.  I struggled with this just last
    > night. The problem is that there is no good hook that I can find in
    > the api for setting up your application delegate to handle
    > applicationWillFinishLaunching in the first place.
    >

    Yes, this is the problem. I have no hook!

    At 12:24 -0600 26/01/01, Eric Peyton wrote:
    > Pick any class in your app.
    >
    > In it's init method do
    >
    > [NSApp setDelegate:self];

    Ok this is the solution! Now I would like to know which methods the
    delegate is in the obligation to implement. I read the documentation
    about NSApplication and it is not clear which methods is optional and
    which methods is not. Where is the documentation telling that?

    Thank you all for your help!

    Tib.
  • They are all optional.  That's the beauty of delegation.  If a class
    requires you to implement one or more of them, as a general rule appkit
    will tell you so either a) in the docs or b) when you run the
    app and don't have them all hooked up.  Always pay attention to the
    console ...

    Eric

    On Fri, 26 Jan 2001, Pierre
    Thibault wrote:

    >> I certainly understand his dilemma.  I struggled with this just last
    >> night. The problem is that there is no good hook that I can find in
    >> the api for setting up your application delegate to handle
    >> applicationWillFinishLaunching in the first place.
    >>
    >
    > Yes, this is the problem. I have no hook!
    >
    > At 12:24 -0600 26/01/01, Eric Peyton wrote:
    >> Pick any class in your app.
    >>
    >> In it's init method do
    >>
    >> [NSApp setDelegate:self];
    >
    > Ok this is the solution! Now I would like to know which methods the
    > delegate is in the obligation to implement. I read the documentation
    > about NSApplication and it is not clear which methods is optional and
    > which methods is not. Where is the documentation telling that?
    >
    > Thank you all for your help!
    >
    > Tib.
    > _______________________________________________
    > MacOSX-dev mailing list
    > <MacOSX-dev...>
    > http://www.omnigroup.com/mailman/listinfo/macosx-dev
    >

    --
    Eric Peyton
    <epeyton...>

    Software and Source for Mac OS X
  • On Friday, January 26, 2001, at 07:53 PM, Pierre Thibault wrote:

    > At 12:24 -0600 26/01/01, Eric Peyton wrote:
    >> Pick any class in your app.
    >>
    >> In it's init method do
    >>
    >> [NSApp setDelegate:self];
    >
    > Ok this is the solution! Now I would like to know which methods the
    > delegate is in the obligation to implement. I read the documentation
    > about NSApplication and it is not clear which methods is optional and
    > which methods is not. Where is the documentation telling that?

    Everything is optional (just like with every other delegate).

    andy

    --
    We fucked up. We fucked up big time.
    -- Steve Jobs
  • On Fri, 26 Jan 2001, Eric Peyton wrote:

    > Pick any class in your app.
    >
    > In it's init method do
    >
    > [NSApp setDelegate:self];
    >
    > add the -application(will/should) methods to that class.
    >
    > You are done.
    >
    > No IB or special equipment required.
    >

    How exactly does the Application check if the Delegate implements
    a particular method ?  -respondsToSelector: ?

    Delegate objects  in Python that define the appropriate methods
    don't get called, as there is a NSProxy bridging python objects
    and objc objects. The proxy object itself does not implement
    applicationDidFinishLaunching et.al. , so the python objects
    that *do* implement them don't get called.

    I can think of two solutions:

    Implement stubs for all of the delegate methods in the proxy
    class, and when they get called, have the proxy check if there
    is a python implementation.

    Have the proxy's respondToSelector: query it's python object.

    Maybe there are others.

    What is the "Right" way?

    What does the java bridge do ?

    -- Steve Majewski <sdm7g...>
  • On Fri, 26 Jan 2001, Eric Peyton wrote:

    >> How exactly does the Application check if the Delegate implements
    >> a particular method ?  -respondsToSelector: ?
    >
    > Yep.

    Thanks.

    >
    > No clue about the rest ofg your message.
    >
    > Eric
    >

    Well -- to try to be more concise and omit the python specific stuff:

    There's a NSProxy sitting between my NSApplication and it's delegate.

    The delegate methods don't get called, I assume,  because they aren't
    actually implemented by the proxy.

    Which is the best lie  to instruct my proxy to tell to make proxied
    delegates work properly?

    -- Steve Majewski
  • On Friday, January 26, 2001, at 10:03 PM, Steven D. Majewski wrote:

    > On Fri, 26 Jan 2001, Eric Peyton wrote:
    >
    >>> How exactly does the Application check if the Delegate implements
    >>> a particular method ?  -respondsToSelector: ?
    > There's a NSProxy sitting between my NSApplication and it's delegate.
    >
    > The delegate methods don't get called, I assume,  because they aren't
    > actually implemented by the proxy.
    >
    > Which is the best lie  to instruct my proxy to tell to make proxied
    > delegates work properly?

    Isn't it obvious to override respondsToSelector:?
    Maybe you should read Apple's ObjC documentation, there's an example how to handle that.
    file:/Developer/Documentation/Cocoa/ObjectiveC/ObjC.pdf, starting at page 144

    andy

    --
    Description forthcoming.
  • On Fri, 26 Jan 2001, Andreas Monitzer wrote:

    > Isn't it obvious to override respondsToSelector:?

    That was my guess, but some of the comments in the NSObject protocol
    docs caused me to think that perhaps that wasn't the appropriate lie.

    > Maybe you should read Apple's ObjC documentation, there's an example
    > how to handle that.
    > file:/Developer/Documentation/Cocoa/ObjectiveC/ObjC.pdf, starting at
    > page 144

    That's more clear but still equivocal: it gives an example where
    respondsToSelector: says NO, even though it can forward and handle
    the message and, after stressing the distinction between inheritance
    and forwarding, then says there are times  where you might want to
    lie about it in respondsToSelector:  and isKindOfClass: .

    ( OK: The doc's  never use the word "lie" , but it was it's stressing
    the distinction that made that characterization come to mind! )

    Since I can think of more than one way to lie about it, I was
    wondering which lie was less likely to cause other problems.

    In other words: The docs make it clear what I *can* do -- what
    wasn't clear was what I *should* do.

    -- Steve Majewski