warning: Creating default object from empty value in /var/www/ on line 33.

NSExpression example

NSExpression is a very powerful class in iOS/OSX that calculate formula. We can also pass arguments or parameters to the formula:

NSExpression *expression1 = [NSExpression expressionWithFormat: @"1+2"];
NSLog(@"expression1 %@", [expression1 expressionValueWithObject:nil context:nil]);
NSExpression *expression2 = [NSExpression expressionWithFormat: @"1+%@-%@" argumentArray:@[@1.2, @2.2]];
NSLog(@"expression2 %@", [expression2 expressionValueWithObject:nil context:nil]);
NSMutableDictionary *context = [NSMutableDictionary dictionaryWithDictionary:

Free XCode plugins for XCode 4

Some opensource free plugins that you can add to your XCode to increase your productivity. All tested on the new XCode 4.6.

Mini Xcode Plugin
You know there is a fat toolbar at the top of the your windows since XCode 4. Although you can hide this tool bar, it will also hide the current project scheme and target. After installed this plugins, it will:
- add a popup menu at the top left or the windows that shows project scheme and target when tool bar is hidden.
- add shortcut (Ctrl+7, Ctrl+8) to select the schemes and targets.

Convert async block to sync

Since iOS 5, many apis will only provide block version and so we will get the result in async order. Sometimes we may want to get the result immediately by scarifying the benefit of GCD.

In old style, we can't do it using NSCondition:

__block NSCondition *condition = [[NSCondition alloc] init];
__block ALAssetsGroup *ret = nil;
ALAssetsLibraryGroupsEnumerationResultsBlock listGroupBlock = ^(ALAssetsGroup *group, BOOL *stop) {
[condition lock];
ret = group;
[condition signal];
[condition unlock];

Set priority in custom queue of GCD

In iOS GCD, you can use GCD queue to serialize operation by using DISPATCH_QUEUE_SERIAL parameters:

queue = dispatch_queue_create("", DISPATCH_QUEUE_SERIAL);

then you can create a queue dispatch like:

dispatch_async(self.renderQueue, ^{
... your code here ...

but the priority of the queue is unknown, so sometimes, when you queue a lot of task to this queue. It still block the foreground task and make the user interface not smooth.

If fix it, we can:

dispatch_async(self.renderQueue, ^{

Behavior changed for parentViewController in iOS 5

Previously, if we present a modal view controller using:

[self presentModalViewController:modalViewController animated: YES]

We should able to get back the parent of the modal view controller using:

[modelViewController parentViewController]

and normally we will dismiss the view controller using:

[[modelViewController parentViewController] dismissModalViewControllerAnimated:YES];

Set the Company Name in XCode 4

In XCode 3.x, we have to set the following company marco so that new source code won't show __MyCompanyName__ in new source code:

defaults write PBXCustomTemplateMacroDefinitions '{ "ORGANIZATIONNAME" = "First Water Tech Ltd"; }'

However, it won't work in XCode 4. According to XCode 4 release note:

Convert between NSData and NSDictionary

We can simplu use NSKeyedArchiver to serialize NSDictionary. To convert NSDictionary to NSData:

NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:yourDictionary forKey:@"Some Key Value"];
[archiver finishEncoding];
[archiver release];
/** data is ready now, and can use it **/
[data release];

To deserialize a NSData:

NSData *data = [[NSMutableData alloc] initWithContentsOfFile:[self dataFilePath]];

Streaming video on android

Playing streaming MP4 video from a URL is a piece of cake in iPhone, but not the case in android. Firstly you have to create your own activity to play video using MediaPlayer class. It is not difficult because there is sample code in the APIDemo projects in the SDK. However, after success to play it on my Nexus One with OS 2.2, I found that it won't play on my G1 with OS 1.6, with error "sorry,this video is not valid for streaming to this Device.."

Manually set the locale of an application in iPhone

The following code will change the locale of the application to Chinese. However, you must set it in the beginning of the app, and it will take effect in next launch of the app.

[[NSUserDefaults standardUserDefaults] setObject: [NSArray arrayWithObject:@"zh"] forKey:@"AppleLanguages"];

Manually set the locale of the application in Android

Android provides a flexible support of localization that system will load different resources according the locale of the current system configuration. However, sometimes we want to manually change the locale of our application especially for our Hong Kong market that people may want to have Chinese locale application in a English system configuration.

To do this, we can set the configuration of the application using the following code:


import android.content.res.Resources;
import java.util.Locale;

Syndicate content