[Solved] When do I use a + or – at the beginning of creating a method? [duplicate]


As has been indicated here the + indicates a class method, and the – indicates an instance method.

The important difference that will likely be relevant to your programming is when to use a class versus and instance method. A class method (+) is simply a method you call using the class. So, something along the lines of [[DataAccess class] getInfoFromServer]. You would want to use a class method when you do not need to access properties but want specific behavior that is related to the class (for example, it makes sense to be calling a DataAccess class to grab information from a server.)

Alternatively, you would want to use an instance method when you want to alloc and initialize an object and then use its properties. So for example, DataAccess * accessObject = [[DataAccess alloc] init] would give you a DataAccess object. That object would presumably have relevant properties which would be declared in the @interface either in your .h file or .m file.

For example, going along with the DataAccess class you would have something like this declared in your .m file before the @implementation.

 @interface DataAccess()
 @property (strong, non-atomic) NSDictionary * data;

 @end

Then in any given instance method you can assume that the object has data set. So you can use calls like self.data in the instance method to grab the data because you assume for the object calling the method the property data has been set. If you use a class method, you cannot make a call to self.data because you are not working with a particular instance of the method.

To put it perhaps over-simply, you can think of class methods as simple functions related to the class where you do not need to access particular properties of an instance of the class, whereas you can think of instance methods as needing an instance of the class to call it, since it needs properties associated with an instance (an allocated object of the class) to work properly.

2

solved When do I use a + or – at the beginning of creating a method? [duplicate]