-1

Possible Duplicate:
What do the plus and minus signs mean in Objective C next to a method?

As I continue to learn about iOS, I came across this, +(CCScene *)function {...} in an early line of the example code I was working with. I understand that methods are implemented inObj-C with a -, as in the following method: -(ObjectType*)function {...}, but I am curious, what does the + that precedes the apparent function implementation indicate?

Community
  • 1
  • 1
Chris Grimm
  • 771
  • 6
  • 15
  • '+' indicate class method and '-' indicates instance method. – Jitendra Singh Jun 03 '12 at 19:35
  • dup of [What do the plus and minus signs mean in Objective C next to a method?](http://stackoverflow.com/questions/2097294/what-do-the-plus-and-minus-signs-mean-in-objective-c-next-to-a-method) – outis Jun 03 '12 at 19:37

2 Answers2

2

Instance methods (or rather messages in Objective-C) start with a -. Class methods start with a +. The difference is, that instance messages can be sent to objects of a certain class, while class messages has be sent to the class itself.

Let's say you have a class called Country. Then you might have an instance message name or area, which will return the name or the area of your Country instance.

NSString *name = [someCountry name];
// name could be "Germany" or "France" for instance.

You might also have a numberOfCountries class message, which will return the total number of Country instances.

int totalCount = [Country numberOfCountries];
DrummerB
  • 39,814
  • 12
  • 105
  • 142
1

'+' indicates a class method and '-' indicates an instance method. Thus, +(CCScene *)function {...} is a class method.

Instance methods operate on an object and has access to its instance variables, while a class method operates on a class as a whole and has no access to a particular instance's variables (unless you pass the instance in as a parameter).

For more information, see "Class vs Instance Methods?" here on Stackoverflow.

Community
  • 1
  • 1
dgund
  • 3,459
  • 4
  • 39
  • 64