Archive for the ‘ Development ’ Category

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];
}

How do I add an existing framework to my iOS project in Xcode4 ?

As a iOS developer, when I migrated to XCode4, it took me a while to know how one can add an existing framework to his/her Xcode iOS project. So, I thought of sharing it with others who may be facing similar problem:

select your project,

  1. select the project on left. You need to go the the panel on the left and select the the topmost item which is your project. This will give you interface that allows you to find a Build Phases tab.
  2. go to the ‘build phases’ tab,
  3. open the ‘Link binary with Libraries’ section
  4. Use the + to add the framework you want

kUTTypeImage ld: symbol(s) not found for architecture armv6

Problem
Use kUTTypeImage in your code. Build fails with following error:
ld: symbol(s) not found for architecture armv6

Solution
Include MobileCoreServices framework and import <MobileCoreServices/UTCoreTypes.h> in the class file.