Re: NSArray as a static
Re: NSArray as a static
- Subject: Re: NSArray as a static
- From: Richard Good <email@hidden>
- Date: Mon, 1 Sep 2008 13:47:02 -0700
Very cogent description, but I'm not trying to access the array from
outside of the class.
I'm trying to create an array of string constants to be used inside
the Person class.
So let me rephrase the question How do I create an array of constant
strings such that I have only one instance for the entire class,
or is that just not possible. If its not possible how do you approach
the problem in Objective C.
I tried Jean_Daniel's suggestion as to incrementing the retain count
to no avail.
I have traced the code and am sure that the initial init is being
called.
I think that for some reason even after changing the retain count the
strings are being freed.
On Sep 1, 2008, at 12:18 PM, Kyle Sluder wrote:
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.
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 (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