Why do my switch cases sometime need braces in Objective-C? -


this question has answer here:

sometimes xcode show error "expected expression" on line after case. example, xcode pointing usercontentviewcontroller red arrow:

- (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath {     switch (indexpath.row) {         case indexvideo:              usercontentviewcontroller* detailviewcontroller = [[usercontentviewcontroller alloc] initwithuser:self.user];             [self.navigationcontroller pushviewcontroller:detailviewcontroller animated:yes];             break;      } } 

if put braces around case, error gone. firstly, want know problem not using braces. i've never used braces in cases in other language. secondly, why xcode complain cases on rare occasions? can't quite discern type of code triggers error.

basically, if want declare variable need add braces define scope.

arc adds requirements (or, rather, stricter requirements) define scope (so may number of "switch case in protected scope" errors fix when upgrading older codebase). because arc needs know in detail when variable isn't / can't referred more can deal release correctly.

everything relates giving compiler enough information scope of declared variables. should part of single case, or multiple cases...


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -