Re: how to centralize functionality more elegantly?
Re: how to centralize functionality more elegantly?
- Subject: Re: how to centralize functionality more elegantly?
- From: James DiPalma <email@hidden>
- Date: Sat, 7 Sep 2002 12:52:19 -0400
Because [NSApp delegate] returns an (id), this code will compile without
warning, that is if showStatus: is defined for any object and known to
gcc when compiling this line of code.
By adding (MainController*), this code gains some type checking that
could raise a compile time warning. However, there is no compile time or
run time check to verify that [NSApp delegate] actually is a
(MainController*).
Two recommendations I have: use a singleton class and change your line
of code to:
[[MainController sharedController] showStatus:@"You cannot do that."];
or use a notification.
A singleton has advantages like type checking and independence from your
MainController being your apps delegate (maybe you'll want to separate
MainController into 2 classes some day). A notification has advantages
like allowing anyone to listen for showStatus: messages (maybe you'll
NSLog status, pop a window for new status, and have a status history
window).
Using a #define would make your code confusion for a 3rd person reading
your code. Maybe you will never have other developers working on your
project and you value reducing your code's word count. But, I've always
disliked using #define and even dislike using typedef for non-structures
(Carbon is plagued with typedefs and I waste too much time trying to
remember and track down Carbon types).*
I don't even use NSApp in my code because someday someone looking at my
code might try to find NSApp in documentation, its not a class but it
looks like one, and PB returns 41 occurrences of NSApp in definitions;
[NSApplication sharedApplication] is clearly a singleton instance of
NSApplication.
-jim
From: Andrew Merenbach wrote:
Is the "(MainController*)" part actually necessary? Some code in my
program works with the equivalent of:
[[NSApp delegate] showStatus: @"You cannot do that."];
Take care,
Andrew Merenbach
From: Matt Neuburg:
[(MainController*) [NSApp delegate] showStatus: @"You cannot do
that."];
Is there a more elegant way to be doing this? m.
* For example I recently tracked down ConstHFSUniStr255Param which is
defined by these typedef statements:
typedef const HFSUniStr255 * ConstHFSUniStr255Param;
struct HFSUniStr255 {
UInt16 length; /* number of unicode
characters */
UniChar unicode[255]; /* unicode characters */
};
typedef struct HFSUniStr255 HFSUniStr255;
typedef UInt16 UniChar;
typedef unsigned short UInt16;
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.