On Mon, Sep 1, 2008 at 2:29 PM, Richard Good <[EMAIL PROTECTED]> wrote:
> What I want is how to use the Java idea of a class static  variable in
> Objective C

Because Objective-C doesn't have class variables (as Jean-Daniel
noted), you have to use a global variable.  The "static" keyword in C
means that the variable has file-level scope.  So in your Person.m
file, you would do something like this:

static NSMutableArray *relationshipMatch;
@implementation Person

+ (NSMutableArray *)relationshipMatch
{
    return relationshipMatch;
}

@end

This will expose the file-scoped array using a class method.  If you
want to have direct access to the variable (violating encapsulation,
but often useful for things like constants) then you can put an extern
declaration in your Person.h like so:

extern NSMutableArray *relationshipMatch;

Then any source file that #imports Person.h will know of the existence
of an NSMutableArray *relationshipMatch, which will be defined when
Person.m is compiled.  The linker will resolve all references to this
variable.

This is how constants are usually implemented.  For example, the
NSPasteboard class uses constants to retrieve different
representations of data on the pasteboard.  An application can put a
plain-text representation and an RTF representation on the pasteboard
so other apps can consume the more appropriate representation.  The
consumer then gets the data from the pasteboard using -[NSPasteboard
dataForType:], passing in NSRTFPboardType for the RTF representation,
or NSStringPboardType for an NSString.  These constants are declared
as externs, like so:

extern NSString *NSRTFPboardType;
extern NSString *NSStringPboardType;

You get these declarations by #importing AppKit.h.  The actual
definitions of these constants happens somewhere in the AppKit source
code, which we don't have access to.  They look something like this
(the contents of the strings doesn't matter):

static NSString *NSRTFPboardType = @"RTF Pasteboard Type Identifier";
static NSString *NSStringPboardType = @"String Pasteboard Type Identifier";

At link time, the linker is able to resolve your code's references to
either of these constants to their definition in the AppKit library.
This is a good pattern to adopt for constants, but for implementing
things like class variables, you might not want to expose the variable
with an extern declaration, but instead by using a class method
accessor.

Hope that explains the concept well enough for you.

--Kyle Sluder
_______________________________________________

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 [EMAIL PROTECTED]

Reply via email to