“Expected Expression” when declaring and initializing NSInteger in a switch statement

I ran across a strange problem today. Tried something like this:

switch(controlVariable) {

case 0:

NSInteger x = kSomeConstant;

// some other code…

break;

case 1:

// …

default:

// …

}

And to my surprise, XCode complained saying that “Parse error: Expected Expression”. Upon googling, this is what I found.

So, the solution is to put some statement before you initialize NSInteger. If there is no statement that logically fit, you can simply insert a semi-colon “;” before declaring and initializing the integer.

switch(controlVariable) {

case 0:

;

NSInteger x = kSomeConstant;

// some other code…

break;

case 1:

// …

default:

// …

}

A Tip on programming Table Views

I found a great post on organizing your code while writing code around Table View/Table View Delegate. It details how one can organize sections and rows by making them enums instead of using numbers all over the delegate methods. See for yourself:

Click the post here.

Solved: putpkt: write failed: Broken pipe

If you see this problem, try following these steps.

a) quit Xcode (and the GDB it is running)
b) unplug device
c) delete app from device (hold icon until the ‘X’ appears)
d) turn off device
e) turn on device
f) plug in device (if itunes wants to sync, let it finish)
g) launch Xcode

Xcode Tip: Organize your source code

One can (IMHO should) take advantage of some of the formatting techniques inbuilt (but obscured from the user) in Xcode.  In this post I will discuss two such techniques:

1. Indentation: So you copy-pasted some code from apple’s tutorials and want to reuse it in your source code.  Oh, but wait ! The formatting is all screwed up. You don’t want to indent every line ! Fortunately, XCode provides a hidden trick to reformat your code. All you have to do is copy your entire source code file and delete it and paste it back in. So, Command-A(select all), Command-C (to copy), Command-V(to paste) and BINGO !

2. Organizing methods (using #pragma): With Delegation, a single view controller in your source code can become a delegate of a lot of other view controllers. As a result, it will contain the delegation methods of all those view controllers. Its a good practice to group the delegation methods together and give that a label so that when you view the method list at top of the source editor, the grouped method will show up together under the group header of your choice. This can be achieved by   using the following syntax in source code:

#pragma mark <some label name>
method 1
method 2
.
.
#pragma mark-

Proper way to show/hide animating Activity Indicator

There are scenarios when one does some heavy weight fetching of data from an external database thats time consuming. During the fetching, we want to give appropriate feedback to user that something is being done. Thats when Activity Indicator (UIActivityIndicator) object comes in handy. Its a spinner that shows and spins and then stops animating and disappears when the process is finished.

A point to be noted is that one should always start Spinner in a separate thread.

Wrong (works only sometimes):

[activityindicator startanimating];
//Some code
[activityindicator stopanimating];

Right

[NSThread detachNewThreadSelector:@selector(threadStartAnimating: ) toTarget:self withObject:nil];
// Some code  
[spinner stopAnimating];
[self.navigationcontroller pushviewcontroller:viewcontroller animated:YES]; 

-(void)threadStartAnimating: (id)data
{
[spinner startAnimating];
}