Saturday, March 17, 2012

The next generation: Objective-C 2.2 or Objective-C 3.0?

According to a recent post by Big Nerd Ranch Weblog, the most recent Clang build now support Objective-C Literal feature.


This is gonna be the most exciting part of this year’s WWDC! I believe Apple will announce this feature in this year’s WWDC. Damn it, I can’t miss that!

What’s Objetive-C Literal syntax feature?

According Ted Kremenek from apple who committed a large patch to llvm repository recently(On March 6th, 2012)

Add clang support for new Objective-C literal syntax for NSDictionary, NSArray, NSNumber, and boolean literals. This includes both Sema and Codegen support. Included is also support for new Objective-C container subscripting.

My apologies for the large patch. It was very difficult to break apart. The patch introduces changes to the driver as well to cause clang to link in additional runtime support when needed to support the new language features.

Docs are forthcoming to document the implementation and behavior of these features.

Is it just like the boxing/unboxing feature in Java?

Yes, it is.

This feature makes Objective-C much more easy to use and much more like a modern high-level programming language while maintain the downward compatibility with C.

NOTE: Java introduced auto boxing/unboxing in 2004 with Java 1.5.0.

How to use Objective-C Literal feature?

Here are some examples:

Before we start to use Objective-C Literal

 NSArray *myArray = [NSArray arrayWithObjects:                     @"string",                      [NSNumber numberWithInt:1234],                     [NSNumber numberWithFloat:12.34],                     [NSNumber numberWithLongLong:123456789012345],                     nil];  NSDictionary *myDict = [NSDictionary dictionaryWithObjectsAndKeys:                         @"Value1", @"key1",                          [NSNumber numberWithInt:1234], @"key2",                          [NSNumber numberWithBool:YES], @"key3",                          nil];  

After using Objective-C Literal

 NSArray *myArray = @[@"string", @1234, @12.34, @123456789012345, nil]; NSDictionary *myDict = @{     @"key1":@"Value1",     @"key2":@1234,     @"key3":@YES };  

Super easy, super fast! right?

What’s the next version number?

Last time Apple updated Objective-C language with block syntax, and a fantastic library for concurrent computing. There is an official version for block, that’s Objective-C V2.1.

This time, it brings some nice features which are common in Java and other dynamic language lick ruby, php, python. What’s the version we should call it?

I predicate that Apple will proudly call it V3.0. Yes, Objective-C V3.0. Let’s wait and see!

Want to have a try?

Goto clang’s page, checkout the most recent source code, build it and try to run a test case!

NOTE: if you downloaded the source in a folder with a space in the name, you might not be able to build the llvm and clang.

So, DO NOT put the source code in a folder with space

Finally, you can reach me at twitter: @tonnyxu or facebook Tonny Xu

Via Tonny Xu iPhone Dev, iPhone Romur, Programming, Something really cool!

No comments:

Post a Comment