When coding Objective-C, is it allowed to do sth. like:
{
UIView *viewOne = [[UIView alloc] init];
// do stuff with view
[self addSubview:viewOne];
[viewOne release];
} //View 1
{
UIView *viewTwo = [[UIView alloc] init];
// do stuff with view
[self addSubview:viewTwo];
[viewTwo release];
} //View 2
When i once did something like this, XCode does not complain and it runs on iOS 4.3.3.
My question is, if this makes my code incompatible to iOS 3.1.3 or something, because this looks to me like it may be blocks i am using.
I’d like to use this mainly for code readability and folding.
All you are doing there is creating separate scopes. Anything declared inside the first scope won’t be available to the second scope.
This is perfectly legal C (and since Objective-C is simply a strict superset of C, its fine there too).
I’ve seen it done in a lot of projects I’ve worked on. However, I’ve never used it myself, as it can be seen as a way of separating parts of a method that aren’t particularly related and can be an indication of poor code quality – not that I’m saying your code is poor!
Also these are not C blocks, as in “closures”, a C block looks like
in it’s most basic form.