Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Instance method is a method you can call on an object (as opposed to a class). Each object is an <em>instance</em> of a certain class (just as each of us is an instance of a human<sup>1</sup>), that’s why we talk about <em>instance methods</em>. For example when you say <code>[someArray count]</code>, you are calling an instance method called <code>count</code> on some array object (= instance of the <code>NSArray</code> class).</p> <p>Class methods are called on classes, for example <code>[UIApplication sharedApplication]</code> is a class method of the <code>UIApplication</code> class. You can’t call an instance method on a class, nor can you call a class method on an object.</p> <p>Category is a way to extend behaviour of existing classes, a kind of plugin you stick on an existing class. For example by writing this:</p> <pre><code>@interface NSObject (SampleCategory) - (void) sayFoo; @end @implementation NSObject (SampleCategory) - (void) sayFoo { NSLog(@"Foo!"); } @end </code></pre> <p>…you make it possible to call <code>[anObject sayFoo]</code> on any object derived from <code>NSObject</code>. And now we are getting to the point of the sentence: <code>NSObject</code> seems to be special, because when you declare an instance method of <code>NSObject</code> using a category (such as our <code>sayFoo</code>), you can call this method even as a class method: <code>[NSObject sayFoo]</code>, <code>[NSView sayFoo]</code>, etc.</p> <hr> <p>1] Sorry, Googlebot!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload