Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You use a <code>*</code> when the variable type is a class.</p> <p>An example may help.</p> <pre><code>NSNumber *number; NSInteger integer; </code></pre> <p>The <code>NSNumber</code> variable type is a class while <code>NSInteger</code> is just another name for a normal C-type <code>int</code>. As you can see here, the compiler replaces every occurrence of <code>NSInteger</code> with <code>int</code>:</p> <pre><code>#if __LP64__ || NS_BUILD_32_LIKE_64 typedef long NSInteger; typedef unsigned long NSUInteger; #else typedef int NSInteger; typedef unsigned int NSUInteger; #endif </code></pre> <hr> <p>Further, you cannot declare an instance of a class(an object), like <code>NSNumber</code>, without using a pointer(thus you use a <code>*</code>). The reason for this is that when you <code>alloc</code> an instance of a class as an object a memory address is returned. A pointer is a type of variable that specifically refers to a memory location. For example:</p> <pre><code>NSNumber *number = [NSNumber alloc]; </code></pre> <p>Here <code>number</code>'s numeric value would be a memory location like <code>0x19a30c0</code>. You could operate on it by adding and subtracting, like an <code>int</code>. The key purpose of declaring it as a <code>NSNumber</code> pointer is so the compiler can help the coder verify that the class has certain methods or to access known properties.</p> <hr> <p>One last example:</p> <pre><code>NSInteger integer = [NSNumber alloc]; </code></pre> <p>What would the value of <code>integer</code> be? In our example, it would be 0x19a30c0. With this you could actually still access the newly allocated <code>NSNumber</code> object via <code>[integer someMethod]</code>. The compiler would give you a warning, though. More over:</p> <pre><code>integer += 4; </code></pre> <p>This would affect the numeric value <code>0x19a30c0</code> by adding 4 to it and making it <code>0x19a30c4</code>.</p> <hr> <p>Look at this Wikipedia article on <a href="http://en.wikipedia.org/wiki/Pointer_(computing)#C_and_C.2B.2B" rel="nofollow noreferrer">C/C++ pointers</a> for some more examples of how, when, and why to use an <code>*</code> operator.</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