Note that there are some explanatory texts on larger screens.

plurals
  1. POIn Objective C, why am I allowed to assign an NSArray to an NSMutableArray without error or warning?
    primarykey
    data
    text
    <p>I'm disturbed by a weird behavior, illustrated by the following example:</p> <pre><code>NSMutableArray *a1 = [[NSMutableArray alloc] init]; // fine NSMutableArray *a2 = [NSMutableArray array]; // fine, too // compiler reports incompatible pointer types; good: NSMutableArray *a3 = [[NSArray alloc] init]; // compiler says nothing and is happy to assign this! NSMutableArray *a4 = [NSArray array]; </code></pre> <p>Both <code>init</code> and <code>array</code> method of both the <code>NSArray</code> and <code>NSMutableArray</code> classes return <code>id</code>. However, the behavior when I call these methods is simply not the same, and clang lets me happily assign an empty <code>NSArray</code> to an <code>NSMutableArray</code> variable!</p> <p>It turns out that <a href="http://clang.llvm.org/docs/LanguageExtensions.html#objc_instancetype" rel="nofollow">clang will automatically change the return type of some methods, including the <code>init</code> family, to <code>instancetype</code></a>, and thus be able to determine at compile time that <code>[[NSArray alloc] init]</code> returns an <code>NSArray *</code> and not an <code>NSMutableArray *</code>. But this check simply doesn't work with the <code>array</code> method.</p> <p>Why? Shouldn't lines like my last example generate <em>at least</em> a warning? Why aren't all these methods declared as returning <code>instancetype</code>? Will it change in the future?</p> <hr/> <p><strong>Update</strong></p> <p>Good news: as of iOS 7, <code>[NSArray array]</code> returns <code>instancetype</code>, so the assignment to <code>a4</code> above also yields a warning. Other methods like <code>arrayWithContentsOfFile:</code> or <code>arrayWithContentsOfURL</code> still return <code>id</code>, though…</p>
    singulars
    1. This table or related slice is empty.
    plurals
    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