What is the difference between methods that are declared with - and methods that are declared with +
e.g
- (void)methodname + (void)methodname
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.
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.
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 .
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.
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"];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With