Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Why is id a weak reference pointer?</p> </blockquote> <p><code>id</code> is not a weak reference pointer, at least not in the ARC ownership sense. Whether an <code>id</code>-typed reference to an object is weak or not depends on the reference having been declared <code>__weak</code> (and variations) and the object’s class actually supporting weak references.</p> <p>However, you could say that <code>id</code> provides <em>weak</em> typing, although I think that dynamic/duck typing is a more accurate description. Since an <code>id</code>- typed reference contains no compile-time class-type information, the compiler isn’t able to, for example, determine if the underlying object can respond to a given selector, which could lead to runtime errors.</p> <blockquote> <p>How is it able to handle any class type pointer?</p> </blockquote> <p>That’s part of the definition of <a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocObjectsClasses.html">the Objective-C language</a>. The compiler recognises <code>id</code> as being the supertype of every Objective-C class, and it treats <code>id</code> differently. See the answer below as well.</p> <blockquote> <p>At runtime, how can we detect that which type of class pointer is assigned to id?</p> </blockquote> <p>In Apple’s Objective-C runtime, the first bytes in the memory allocated to an object must point to that object’s class. You might see this referenced elsewhere as the <code>isa</code> pointer, and that’s how Apple’s runtime finds out the class of every<sup>1</sup> object. The <code>id</code> type is defined to have this information as well. In fact, its only attribute is the <code>isa</code> pointer, which means that all<sup>1</sup> Objective-C objects conform to this definition.</p> <p>If you have an <code>id</code> reference and want to discover the class of the referenced object, you can send it <code>-class</code>:</p> <pre><code>id someObject; // Assign something to someObject // Log the corresponding class Class c = [someObject class]; NSLog(@"class = %@", c); // Test whether the object is of type NSString (or a subclass of NSString) if ([someObject isKindOfClass:[NSString class]]) { NSLog(@"it's a string"); } </code></pre> <p><sup>1</sup><a href="http://objectivistc.tumblr.com/post/7872364181/tagged-pointers-and-fast-pathed-cfnumber-integers-in">Tagged pointers</a> are a notable deviation of this structure, and (partly) because of them one shouldn’t access the <code>isa</code> pointer directly.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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