Re: Arc: Switch case is in protected scope
Re: Arc: Switch case is in protected scope
- Subject: Re: Arc: Switch case is in protected scope
- From: Daniel Höpfl <email@hidden>
- Date: Tue, 22 Oct 2013 10:31:01 +0200
Hi,
On 2013-10-22 09:41, Gerriet M. Denkmann wrote:
Converting to Arc (Xcode 5.0).
This works fine without Arc (regardless whether TRIGGER_ERROR is
defined or not):
NSMutableString *mus = [ NSMutableString string ];
NSString *word = @"abc";
switch( self.colourType )
{
case colour_link:
[ mus appendString: @"<a href=\"" ];
#define TRIGGER_ERROR
#ifdef TRIGGER_ERROR
NSString *urlStr = [ word stringByAddingPercentEscapesUsingEncoding:
NSUTF8StringEncoding ];
[ mus appendString: urlStr ];
#else // no_ERROR
[ mus appendString: [ word
stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding ] ];
#endif // no_ERROR
break;
default:
[ mus appendString: word ];
};
But with Arc, if TRIGGER_ERROR is defined, I get an error: "Switch
case is in protected scope" with the further explanation: "Jump
bypasses initialization of retaining variable".
Was the old (non-arc) code faulty (but the compiler did not notice
this)?
Why is the arc-version (with TRIGGER_ERROR defined) wrong?
It is wrong in the non-arc world, too. (ISO/IEC 9899:2011 AKA C11,
6.8.6.1: "A goto statement shall not jump from outside the scope of an
identifier having a variably modified type to inside the scope of that
identifier" - switch is a special case of goto.)
In other words: urlStr is declared and initialized in the
colour_link-case:. Since there is no scope limiting block in this case:,
urlStr is in scope in the default-case. Now imagine urlStr was used in
the default-case: What value would you expect it to have?
Sure, the compiler could detect that urlStr is never used in your
default: (and/or that there is no way to get to default: from
colour_link-case:) but I'm not sure if this can be done in all cases.
Bye,
Daniel
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden