Re: NSArray as a static
Re: NSArray as a static
- Subject: Re: NSArray as a static
- From: Jean-Daniel Dupas <email@hidden>
- Date: Mon, 1 Sep 2008 22:47:40 +0200
Le 1 sept. 08 à 21:18, Kyle Sluder a écrit :
On Mon, Sep 1, 2008 at 2:29 PM, Richard Good <email@hidden>
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.
It will not compile:
extern int bar;
static int bar = 5;
foo.c:2: error: static declaration of ‘bar’ follows non-static
declaration
foo.c:1: error: previous declaration of ‘bar’ was here
To be able to access globals from an other file, they have to be
global variables and not static variables, and should be defined like
this (without the static keyword):
NSString *NSRTFPboardType = @"RTF Pasteboard Type Identifier";
NSString *NSStringPboardType = @"String Pasteboard Type Identifier";
_______________________________________________
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