Re: Release compile failure
Re: Release compile failure
- Subject: Re: Release compile failure
- From: Uli Kusterer <email@hidden>
- Date: Mon, 14 Jan 2008 09:50:19 +0100
On 13.01.2008, at 05:04, Paul wrote:
The problem was that I was initializing these values in my header
files. I don't know why I thought that was a good idea, but it
seemed to make sense at the time.
Actually, the problem was that you were *defining* them in your
header files, whereas header files should only contain *declarations*.
A declaration is like a function prototype: It promises the compiler
that, somewhere, there is a global variable of a certain name. A
definition is the place where the actual variable is created.
In general, you only want to declare your variables in the header,
which is generally done by writing:
extern MyType gMyVar;
Then, in the actual implementation file, you'll actually create and
initialize (i.e. define) your variable:
MyType gMyVar = SomeValue;
Oh, and I suggest you adhere to the naming conventions that exist
there, that is, either start global variable names with a certain
prefix (like "g") to indicate this is a global, or with the same
prefix you use for your classes, i.e. PCMyVar or whatever. Since
globals are in the global namespace, you otherwise run the risk of
name collisions with globals in other files.
Of course, the best approach is to not expose globals to the outside
directly, and instead use a static variable in your source file that
is accessible through accessors:
Header:
@interface MyClass : NSObject
{
}
+(MyType) myVar;
+(void) setMyVar: (MyType)val;
@end
Implementation:
static MyVar sMyVar = SomeValue;
@implementation MyClass
+(MyType) myVar
{
return sMyVar;
}
+(void) setMyVar: (MyType)val
{
sMyVar = val;
}
@end
This also has the advantage that these accessors can fire off
notifications or talk to delegates to update any GUI state that may be
connected with these values, or that you can completely change your
internal implementation to use different types, while still keeping
these methods intact. Especially in a big project it's often handy to
not have to go through and change all the old places that used to call
a method.
Cheers,
-- M. Uli Kusterer
"The Witnesses of TeachText are everywhere..."
http://www.zathras.de
_______________________________________________
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