Archive

Archive for February, 2011

Check if view is visible

if (self.navigationController.visibleViewController == self) {}
Advertisements
Categories: CodeSnippets, Development, iOS

Exceptions and error handling in Objective-C

Compiler directives that are used for exception handling:

1. @try: block of code that can throw an exception.

2. @catch: define block of code to handle exception thrown by try block, this is usually an NSException object.

3. @finally: defines a block of code that executed whether an exception is thrown or not.

4. @throw: once your program detects an exception, it must propagate the exception to code that handles it. This code is called the exception handler. This entire process of propagating an exception is referred to as “throwing an exception”.

@try {
   NSLog(@"trying...");
   [object objectForKey:@"yeah"];
}
@catch (NSException * e) {
   NSLog(@"catching %@ reason %@", [e name], [e reason]);
}
@finally {
   NSLog(@"finally");
}

For more informations have a look at the documentation here.

Categories: Development, iOS, Objective-C

Three Apple Engineers and Three Microsoft Engineers

Three Apple engineers and three Microsoft engineers are traveling by train to a conference. At the station, the three Microsoft engineers each buy tickets and watch as the three Apple engineers buy only a single ticket. “How are three people going to travel on only one ticket?” asks a Microsoft engineer. “Watch and you’ll see,” answers the Apple engineer.

They all board the train. The Microsoft engineers take their respective seats but all three Apple engineers cram into a rest room and close the door behind them. Shortly after the train has departed, the conductor comes around collecting tickets. He knocks on the rest room door and says, “Ticket, please.” The door opens just a crack and a single arm emerges with a ticket in hand. The conductor takes it and moves on. The Microsoft engineers saw this and agreed it was quite a clever idea. So after the conference, the Microsoft engineers decide to copy the Apple engineers (as they always do) on the return trip and save some money.

When they get to the station, they buy a single ticket for the return trip. To their astonishment, the Apple engineers don’t buy a ticket at all. “How are you going to travel without a ticket?” asks one perplexed Microsoft engineer. “Watch and you’ll see,” answers an Apple engineer. When they board the train the three Microsoft engineers cram into a rest room and the three Apple engineers cram into another one nearby. The train departs. Shortly afterward, one of the Apple engineers leaves his rest room and walks over to the rest room where the Microsoft employees are hiding. He knocks on the door and says, “Ticket, please…”

Anonymous

Categories: Jokes, Misc

NSString Basics

Creating strings

NSString *myFirstString = @"foo";
NSString *mySecondString = [[NSString alloc] initWithFormat:
   @"%@%@%@i", myFirstString, @"bar", 1];

Comparing strings

NSString *myString = @"foo";
if([myString isEqualToString:@"foo"]) {
   NSLog (@"Strings are equal!");
}

Finding strings within strings

NSString *myString = @"foo";
NSString *searchForMe = @"Howdi";
NSRange range = [myString rangeOfString : searchForMe];
int location = range.location;
int length = range.length;

if (location != NSNotFound) {

   NSString *locationAndLength = [[NSString alloc] initWithFormat:
      @"Location: %i, length: %i", location, length];
   NSLog(@"I found something.");
   NSLog(locationAndLength);
}

Replacing strings within strings

NSString *myString = @"foo";
myString = [myString stringByReplacingOccurancesOfString:@"oo"
   withString:@"uu"];

Extracting substrings from strings

There are 3 methods that allow to extract substrings from a parent string:

  • -substringToIndex:
  • -substringWithRange:
  • -substringFromIndex: (which respectively take a substring from the beginning, middle, and end of a parent string)

The first method substringToIndex returns a new string which is composed of the characters from the beginning of the receiver string up to (but not including) the character at the specified index:

NSString *aString = @"Running out of ideas for strings.";
NSString *substring = [aString substringToIndex:7];
// result: @"Running"

The method substringFromIndex works in the same way, except now the substring starts at the specified index of the receiver (including the character at the index) and includes all the characters to the end of the receiver:

NSString *substring = [aString substringFromIndex:25];
// result: @"strings"

Finally, we have the method which lets us arbitrarily extract a substring from anywhere within the parent string substringWithRange. The argument to this method is an NSRange:

NSString *substring = [aString substringWithRange:NSMakeRange(15, 5)];
// result: @"ideas"

Here the range starts with the 15th character, “i”, and extends to include the next four characters, giving us a length of 5, “ideas”.

How to convert CHM files into PDF files

There is a Python script called chm2dpf that converts CHM files into PDF files. To install it in Ubuntu use the follwing command:

sudo aptitude install chm2pdf

Using chm2pdf to convert a chm book:

chm2pdf --book my-file.chm my-file.pdf

Categories: Misc, Ubuntu

Dropbox Error: “Can’t sync XYZ; directory not empty”

February 7, 2011 12 comments

How to fix the “Can’t sync XYZ; directory not empty” problem:

1. Stop Dropbox

2. Delete the database dropbox.db and the cache directory in the directory “C:\Documents and Settings\<username>\Application Data\Dropbox” or “C:\Users\<username>\AppData\Roaming\Dropbox” (Win 7).

3. Restart DropboxDropbox will rebuild the cache and database next time it starts.

Categories: Misc

WARNING: Application does not specify an API level requirement!

You have to add the following line in your manifest:

<uses-sdk android:minSdkVersion=”8″ />

The following table specifies the API Level supported by each version of the Android platform.

Platform Version API Level
Android 4.1.1

Android 4.1

16
Android 4.0.4

Android 4.0.3

15
Android 4.0.2

Android 4.0.1

Android 4.0

14
Android 3.2 13
Android 3.1.x 12
Android 3.0.x 11
Android 2.3.4

Android 2.3.3

10
Android 2.3.2

Android 2.3.1

Android 2.3

9
Android 2.2.x 8
Android 2.1.x 7
Android 2.0.1 6
Android 2.0 5
Android 1.6 4
Android 1.5 3
Android 1.1 2
Android 1.0 1

For more informations have a look at http://developer.android.com/guide/appendix/api-levels.html.

Categories: Android, Development