2012-05-19

GitHub Widget

GitHub Widget is a blog widget. It provides displaying that on your web the repositories on GitHub.
It uses JSONP to load data from GitHub (GitHub API).

demo


2012-05-13

High Performance Hex Encode in Objective-C

If you want to write a hex encode function.
You can use for loop and appendFormat:@"%02x" to convert array data to hex string.
[NSMutableString appendFormat:@"%02x", sourceData[index]];

[NSMutableString appendFormat:@"%02x", sourceData[index]]

// download image data
NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://1.bp.blogspot.com/-qZ-dUpOD5VU/T5f9_shfn6I/AAAAAAAAJF8/f10BsQL515U/s1600/ror12.png"]];

// Objective-C ---------------------------
// process timer
NSDate *count = [NSDate date];
// hex encode result
NSMutableString *hex = [[[NSMutableString alloc] init] autorelease];
// convert imgData(NSData) to char[]
unsigned char *unsignedCharData = ((unsigned char *)[imgData bytes]);

for (uint index = 0; index < [imgData length]; index++) {
    // append value to result data
    [hex appendFormat:@"%02x", unsignedCharData[index]];
}
unsignedCharData = nil;
// print process time
NSLog(@"cost : %f ms", [count timeIntervalSinceNow] * -1000.0);
NSLog(@"imgSize : %i kB", [imgData length] / 1024);
NSLog(@"imgData : %@.....", [hex substringToIndex:50]);
// -------------------------------------

2012-05-10

Objective-C Automatic Reference Counting

Objective-C Automatic Reference Counting was added since iOS 5 SDK.
You do not to release object by yourself when you enable ARC(Automatic Reference Counting) on the project.

If you enable ARC on the old project, the compiler will show a lot of error, because of using release/autorelease message in the code.

If some files don't support ARC

Add -fno-objc-arc into Compiler Flags. The compiler will compile the file with disabled ARC.

2012-05-09

Inject value from NSDictionary to custom class - ValueInjector

ValueInjector includes two function injecting value from NSDictionary to custom class and initing NSDictionary with custom class.

After import ValueInjector.h, NSObject will be add a new message "injectFromObject", and NSDictionary will be add a new message "initWithObject".

injectFromObject converts weak typing to strong typing. When you use JSONKit parsing json then get a NSDictionary object, you can use injectFromObject to convert NSDictionary to custom class.

initWithObject converts custom class to NSDictionary. When you want to serialize custom class(strong typing), you can use initWithObject to init NSDictionary with custom class.

Cocoa JSONSerializer - JSONKit

I had posted a content that is about JSON serializer in Cocoa Framework in last year (here).
Is is use SBJson (json-framework).

But there are too meny files in SBJson. Then I found JSONKit a few days ago.
JSONKit is very clean. Just only two files, JSONKit.h and JSONKit.m.
The author of JSONKit johnezang says JSONKit is the fastest JSON Serializer in Cocoa Framework.

Parsing:

2012-04-30

.NET Thread Pool

Multithreaded help application to upgrade performance, but too many threads will reduce performance.
There is a program, it has a main thread. Main thread scans the folder and creates threads to do something. If the main thread finds 100 folders, and creates 100 threads in the same time. Then CPU will run 100 threads in the same time.

ThreadPool Class provides a pool of threads that can be used to execute tasks, post work items, process asynchronous I/O, wait on behalf of other threads, and process timers.

2012-04-29

Parsing HTML in iOS App

We can use topfunky-hpple and libxml2 to parser html in Cocoa Framework.
(libxml2-wiki, libxml2-homepage)

1. Add「-lxml2」into Other Linker Flags