New Objective-C syntax features brought by LLVM compiler 4.4 in Xcode 4.0

Source: Internet
Author: User
Document directory
  • 4.1. NSNumber type
  • 4.2. NSArray type

At this year's WWDC 2012 Apple's global Developer Conference, Apple made significant improvements to LLVM compilers, And the LLVM compiler in the new version of Xcode 4.4 was upgraded to 4.0, it brings many surprising features to Objective-C.

Note: The following syntax requires downloading Xcode 4.4.

1. Enumeration type change

Old Syntax:

typedef enum Week{    Moday,    Tuesday,    Wednesday,    Thursday,    Friday,    Saturday,    Sunday    }Week;

The problem with the old method is that the data range of enumeration values is fuzzy, which may be very large, negative, and cannot be defined.

New statement:

typedef enum Week:NSUInteger{    Moday,    Tuesday,    Wednesday,    Thursday,    Friday,    Saturday,    Sunday    }Week;

The new method binds the enumerated data type NSUInteger while listing the enumerated content. This brings enhanced type check and better code readability.

2. The placement order of the used method code is irrelevant.

If the method is not declared in the. h file, a warning may be triggered if the method is not in the front.

For example:

@interface MyClass : NSObject-(void)doSomething:(NSString *) print;@end

Implementation:

@implementation MyClass-(void)doSomething:(NSString *)print{    NSLog(@"%@", [print stringByAppendingFormat:[self getString]]);}-(NSString *)getString{    return@"string for something";}@end

During early compilation, the following occurs: warning: instance method '-getString:' not found...

The new compiler will first scan the methods in the code and then compile the Code. This avoids the problem that the method cannot be found.

3. Simplified property attributes

@ Property is quite familiar to programmers who use Objective-C. property can automatically generate getter and setter variables. After being declared in the. h file, add the @ synthesize keyword to The. m file to complete the automatic getter and setter processes.

For example, I wrote

@ Property (strong, nonatomic) NSDictionary * order;

I want to write it in the. m file.

@ Synthesize order;

Do you feel a lot of surplus? Now you don't need to write this line of code in the new syntax features. The new version of the compiler helps you implement this line of code, which is called helping people to the end.

That is to say, you are in. after declaring the order attribute in the H file, you can directly use the getter and setter methods of this attribute in the implementation file. the compiler will automatically determine whether to provide the setter Method Based on the readable and writable attributes. More intelligent.

4. Syntax simplification

All those who have done java or C # development know that initialization or assignment of a variable is usually done with a "=" sign. After Objective-C, each time, a long function is used to assign values for live initialization. Now it is much simpler.

Let's take a look at the comparison between the simplified and simplified data types.

4.1. NSNumber type

Old Syntax:

    NSNumber *number;    number = [NSNumber numberWithChar:'X'];    number = [NSNumber numberWithInt:12345];    number = [NSNumber numberWithUnsignedLong:12345ul];    number = [NSNumber numberWithLongLong:12345ll];    number = [NSNumber numberWithFloat:123.45f];    number = [NSNumber numberWithDouble:123.45];    number = [NSNumber numberWithBool:YES];

New statement:

    NSNumber *number;    number = @'X';    number = @12345;    number = @12345ul;    number = @12345ll;    number = @123.45f;    number = @123.45;    number = @YES;

4.2. NSArray type

Old Syntax:

    NSArray *array;    array = [NSArray arrayWithObjects:@"object1", @"object2", @"object3", nil];

New statement:

    NSArray *array = @[ @"object1", @"object2", @"object3" ];

The new method removes the annoying nil.

4.3 NSDictionary type

Old Style

    NSDictionary *dict = [NSDictionary dictionaryWithObjects:@[@"value1", @"value2", @"value3"]                                       forKeys:@[@"key1", @"key2", @"key3"]];

New Writing Method

    NSDictionary *dict = @{@"key1": @"value1",@"key2": @"value2",@"key3": @"value3" };    NSLog(@"%@", dict);

The running result is normal:

{

Key1 = value1;

Key2 = value2;

Key3 = value3;

}

5. Quickly locate objects by subscript

They said that the new syntax is okay, and arrays and tokens can be accessed through subscripts,

NSArray * array = @ [@ "object1", @ "object2", @ "object3"]; id obj = array [0]; // obtain the array object by subscript, replace the original Syntax: array objectAtIndex: I]; NSString * obj1 = @ "oooo"; array [0] = obj1; // You can also assign values to the array object directly. Original replacement Syntax: [array replaceObjectAtIndex: I withObject: newObj]; NSDictionary * dict ={ @ "key1": @ "value1", @ "key2": @ "value2 ", @ "key3": @ "value3"}; id obj2 = dict [@ "key1"]; // obtain the o2 object. Replace the original statement with [dic objectForKey: k2]. dict [@ "key2"] = obj; // assign a value to the object whose key is k2 again. Replace the original Syntax: [dic setObject: newObj forKey: k2]

However, the fact is as follows:


Reality is always cruel. So I googled and found that this syntax is for iOS 6 or OS x 10.8 SDKs. I don't have ios 6 simulators, but I don't have 10.8 SDKs. So an error is reported. See here: http://stackoverflow.com/questions/11425976/compiler-error-expected-method-not-found-when-using-subscript-on-nsarray

The following are some new syntax Features of The WWDC 2012 video:

You can explain how the new syntax feature compiler is implemented.

Copyright Disclaimer: This article will be published at http://blog.csdn.net/totogo2010/, and you will be welcomed to enjoy the transfer and sharing. Please respect the work of the author. Keep this note and the author's blog link when reprinting. Thank you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.