New objective-C syntax features brought by llvm compiler 4.4 in xcode 4.0

Source: Internet
Author: User

note that the following syntax requires downloading xcode 4.4.
1. Enumeration type change
old Syntax:
[CPP] view plaincopy
typedef Enum week {
moday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
} week;
the problem with the old method is that the data range of the enumerated values is fuzzy. This value may be very large, negative, and cannot be defined.

New statement:
[CPP] view plaincopy
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 betterCodeReadability.

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:
[CPP] view plaincopy
@ Interface myclass: nsobject
-(Void) dosomething :( nsstring *) print;
@ End

Implementation:
[CPP] view plaincopy
@ 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
@ PropertyProgramIt is quite familiar to the staff, and property facilitates the automatic generation of 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:
[CPP] view plaincopy
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:
[CPP] view plaincopy
Nsnumber * number;
Number = @ 'X ';
Number = @ 12345;
Number = @ 12345ul;
Number = @ 12345ll;
Number = @ 123.45f;
Number = @ 123.45;
Number = @ Yes;

4.2. nsarray type
Old Syntax:
[CPP] view plaincopy
Nsarray * array;
Array = [nsarray arraywithobjects: @ "object1", @ "object2", @ "object3", nil];

New statement:
[CPP] view plaincopy
Nsarray * array = @ [@ "object1", @ "object2", @ "object3"];
The new method removes the annoying nil.

4.3 nsdictionary type
Old Style
[CPP] view plaincopy
Nsdictionary * dict = [nsdictionary dictionarywithobjects: @ [@ "value1", @ "value2", @ "value3"]
Forkeys: @ [@ "key1", @ "key2", @ "key3"];
New Writing Method
[CPP] view plaincopy
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,

[CPP] view plaincopy
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 a value to an array object. Replace original 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 format with [DIC objectforkey: k2];
dict [@ "key2"] = OBJ; // assign a value to the object whose key is K2 again. Replace the original syntax with [DIC setobject: newobj forkey: k2]

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

 

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.