• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Class variable?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Class variable?


  • Subject: Re: Class variable?
  • From: "Erik D. Holley" <email@hidden>
  • Date: Fri, 6 Feb 2004 14:23:16 -0700

Randall,

Objective-C doesn't facilitate class variables. You'll either need to use a global variable for each class or use a static local variable somewhere within your class implementation.

Using a globally scoped variable is often more straight forward:

// header file
@interface MyClass : NSObject
{
...
}
- (id)init;
- (id)var;
@end

// source file

id MyClass_classVariable = nil;

@implementation MyClass
- (id)init
{
if(self = [super init]) {
MyClass_classVariable = @"hello";
}
return self;
}
- (id)var
{
return MyClass_classVariable;
}
@end


(You'll need to add deallocation of the MyClass_classVariable at the appropriate time.)

-Erik


On Feb 6, 2004, at 1:52 PM, Randall Meadows wrote:

[Man, I hope this isn't a stoopit question...]

Is there a way in Cocoa to define a class variable that is shared among all instances of that class, analogous to a +method? How?

What I want to do is basically create a class that doles out instances of a Carbon structure, and include retain/release capability using reference counting. I was thinking of keeping a NSDictionary of the structures I create, using the address as the key, and another NSDictionary for the counts, similarly keyed.

Am I re-inventing the wheel? Am I just too ignorant about Cocoa to know how to do this with a simple NSObject subclass?


[PS-What are the feelings about cross-posting to cocoa-dev and macosx-dev? I'm new to both, so I don't have a feel for any "turf war" that might be going on...]
_______________________________________________
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.



--
Erik D. Holley
email@hidden
_______________________________________________
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.

References: 
 >Class variable? (From: Randall Meadows <email@hidden>)

  • Prev by Date: Re: Optimisation [inlining] of message calls (was Re: Multiple problems caused by changing NSThreadsleepUntilDate interval)
  • Next by Date: Re: custom webview dragged links
  • Previous by thread: Class variable?
  • Next by thread: Re: Class variable?
  • Index(es):
    • Date
    • Thread