Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>In the old days, we were trained to write init methods like this:</p> <p>Foo.h:</p> <pre><code>-(id) initWithInteger: (NSInteger) someNumber; </code></pre> <p>Foo.m:</p> <pre><code>-(id) initWithInteger: (NSInteger) someNumber { if ((self = [super init])) { doSomeStuff; } return self; } </code></pre> <p>But those id casts are now no-nos. So how should these methods now look?</p> </blockquote> <p>Those aren't casts. In that context, that simply syntax declares the return type of the method, as the <code>(NSInteger)</code> part later on declares the type of that argument.</p> <p>The syntax for declaring a method's return type has not changed under ARC. The code you showed would remain exactly the same.</p> <p>Casts are expressions, so a cast of the sort prohibited by ARC can only occur in expression context. For example, if you said:</p> <pre><code>- (id) initWithCFThing:(CFThingRef)thing { if ((self = [super init])) { _thing = (NSThing *)thing; //Casts expression of type CFThingRef to object pointer type NSThing *; this is invalid } return self; } </code></pre> <p>The cast in the assignment expression is invalid, because it casts from a CF object to an Objective-C object pointer (a “retainable object pointer”) with no bridge keyword. The inverse would likewise be invalid:</p> <pre><code>- (CFThingRef) CFThing { return _thing; //Implicitly casts expression of object pointer type NSThing *to type CFThingRef; this is invalid } </code></pre> <p>Same problem in the other direction: The expression given to the <code>return</code> statement casts a retainable object pointer to a CF object type with no bridge keyword.</p> <p>Note where the errors lie. In each case, the error is in an expression: in <code>initWithCFThing:</code>, it's the explicit cast-expression inside the assignment expression; in the accessor <code>CFThing</code>, it's the implicit cast of the expression inside the <code>return</code> statement to the method's return type.</p> <p>In both cases, as in your case, the prototype of the method is completely valid. <code>(CFThingRef)</code> and <code>(id)</code> are still valid, with or without ARC, when you're declaring a return type or argument type for a method. Only as a cast expression is either syntax invalid, because the cast itself is invalid.</p> <p>Adding a bridge keyword satisfies the compiler in a cast expression, because then you're telling it what you want it to do (bridge-crossing casts with no bridge keyword are invalid because the compiler would have to guess what you mean, and it refuses), but would be invalid in a type declaration because that is not ambiguous. The bridge crossing happens at the cast expression (when there is one), not at an argument passage or return.</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