Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between + and - methods in Objective-c

What is the difference between methods that are declared with - and methods that are declared with +

e.g

- (void)methodname  + (void)methodname 
like image 694
Alex Avatar asked Feb 10 '10 16:02

Alex


People also ask

What are methods in Objective-C?

In Objective-C terms, one object sends a message to another object by calling a method on that object. Objective-C methods are conceptually similar to standard functions in C and other programming languages, though the syntax is quite different.

What is the difference between method that begin with and -?

With the class based function, you just call directly, but you don't have access to any local variables besides #defines that you can do because the class isn't instantiated. But with the - (NSString you must instantiate the class before use, and you have access to all local variables.

What is the difference between class and instance methods?

Key Takeaways. Instance methods need a class instance and can access the instance through self . Class methods don't need a class instance. They can't access the instance ( self ) but they have access to the class itself via cls .

What is the difference between category and extension in Objective-C?

Category and extension both are basically made to handle large code base, but category is a way to extend class API in multiple source files while extension is a way to add required methods outside the main interface file.


1 Answers

Methods prefixed with - are instance methods. This means they can only be invoked on an instance of a class, eg:

[myStringInstance length]; 

Methods prefixed with + are class methods. This means they can be called on Classes, without needing an instance, eg:

[NSString stringWithString:@"Hello World"]; 
like image 105
Jasarien Avatar answered Oct 07 '22 16:10

Jasarien