Question

Until now, I would like to know the difference between these 2. I always been using instance methods but have no idea the meaning behind it. Can anyone explain in the simplest way? Thanks.

Was it helpful?

Solution

Class methods are called on the classes themselves, like this:

[NSDate date];
// declared as: + (NSDate *)date;

Instance methods are called on actual objects:

NSDate *date = ...;
[date timeIntervalSinceNow];
// declared as: - (NSTimeInterval)timeIntervalSinceNow;

Read the The Objective-C Programming Language guide for more information.

OTHER TIPS

Well class methods can be used without making an instance of a class. Since you don't have an instance of this class you can't use any class instance variables.

ex:

@implementation MyStringHelper


@synthesize lastChecked;

+ (BOOL) checkIfEmptyString:(NSString *)checkString {
  return ([checkString length] == 0);
}
@end

Thus you can call this like:

if ( [MyStringHelper checkIfEmptyString:@"NotEmprty"] ) {
// do something
}

But you can't use the properties latChecked because this will need an instance of the MyStringHelper class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top