by admin November 1, 2012 , In iPhone / iPad / iPod

iOS Development – Use of Tilde Sign (~) in File Name

Apple iOS development is a relatively new field of development, and there are a number of tips and tricks that developers can use to create better apps in lesser time. In this blogpost, I will talk about how to use the tilde (~) sign in a way that reduces your coding work.

Use of Tilde Sign in iOS programming for faster coding

Use of Tilde Sign in iOS programming for faster coding

While creating universal app for the iPhone and iPad, writing conditional code can take up a lot of time and energy. Thankfully, there is a simpler way in which you can achieve the same results. By giving file names with a tilde (~) sign, you can save your development efforts with ease.

Just use:

MyCtrlVC~iphone.XIB and MyCtrlVC~ipad.XIB

Let’s dig into the details. While developing universal apps, we have to write a conditional code for each device – the iPad as well as the iPhone. In this scenario, the proper use of tilde can be extremely beneficial.

For example, if you want to push new view controller, then you’d have to write lot of lines (almost 10) of code:

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)

{

MasterViewController *masterViewController = [[MasterViewController alloc] initWithNibName:@”MasterViewController_iphone” bundle:nil];

[self.navigationController pushViewController:masterViewController animated:YES];

[masterViewController release];

}

else

{

MasterViewController *masterViewController = [[MasterViewController alloc] initWithNibName:@”MasterViewController_ipad” bundle:nil];

[self.navigationController pushViewController:masterViewController animated:YES];

[masterViewController release];

}

But here, the magical (~) will help you. You just have to give XIB name with tilde instead of underscore:

MasterViewController~iphone.XIB  and MasterViewController~ipad.XIB

Now you’ll have to write only a few lines of code: Only 4 lines

MasterViewController *masterViewController = [[MasterViewController alloc] initWithNibName:@”MasterViewController” bundle:nil];

[self.navigationController pushViewController:masterViewController animated:YES];

[masterViewController release];

 

Isn’t that amazing? A single tilde will help you save a lot of time and effort. It’s almost magical. Start using it in your projects from now onwards! The best part is that this trick works for all kinds of files that were added in the source code, including image files like .png and .jpeg.