• 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: Instance methods VS. Class methods
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Instance methods VS. Class methods


  • Subject: Re: Instance methods VS. Class methods
  • From: Mike Shields <email@hidden>
  • Date: Sat, 24 Nov 2001 02:07:14 -0700

On Saturday, November 24, 2001, at 12:51 AM, Andreas Monitzer wrote:


The following is valid code (but results in a warning for whatever reason):

@interface MyObject: NSObject {
int value;
}

+ (void)initialize;
@end

@implementation MyObject

+ (void)initialize {
value=10;
}

@end



It's giving a warning because you are trying to access an instance variable from a class method. If you were wanting something similar to C++'s class variables, then you would do something like this:

@interface MyObject: NSObject {
}

+ (void)initialize;
@end

@implementation MyObject

static int value;

+ (void)initialize {
value=10;
}

@end

In this case it's not quite the same since the static int value; is just a plain old C static variable (limited to scope in this compilation unit), but for most purposes, accomplishes the same thing.

Mike


References: 
 >Re: Instance methods VS. Class methods (From: Andreas Monitzer <email@hidden>)

  • Prev by Date: Re: Instance methods VS. Class methods
  • Next by Date: Re: NSCoder and archiving
  • Previous by thread: Re: Instance methods VS. Class methods
  • Next by thread: Re: Instance methods VS. Class methods
  • Index(es):
    • Date
    • Thread