Valid objective-C 2.0 Note 3 (literal syntax short syntax)

Source: Internet
Author: User
When using objective-C, you will always encounter some classes in the foundation framework. These classes include nsstring, nsnumber, nsarray, and nsdictionary. These data structures are self-explanatory. Objective-C is famous for its concise and specific syntax. Since oc1.0 has a simple way to define an nsstring variable, we can declare a string variable nsstring * [email protected] "Hello lves" in this way. before such a syntax is available, we need to create a string variable alloc and then init. Literal (concise) syntax has been extended to nsnumber, nsarray and nsdictionary after oc2.0. Using concise syntax can reduce the amount of code and make it easier to read. The literal number nsnumber variable can encapsulate some basic type variables, such as int, float, and bool. Before using literal, you may define nsnumber * somenumber = [nsnumber numberwithint: 1]; integer 1 is put into nsnumber, of course there are better methods than this, instance:
 NSNumber *[email protected];    NSNumber *[email protected];    NSNumber *[email protected];    NSNumber *[email protected];    NSNumber *[email protected]‘a‘;

The concise syntax can also be expressed as follows:
    int x=5;    float y=6.32f;    NSNumber *[email protected](x*y);
Defining the nsnumber variable as above rather than using the previous syntax is very useful.
Literal arrays are commonly used. Before 2.0, we can define an array as follows:
NSArray *animals=[NSArray arrayWithObjects:@“cat”,@“dog”,nil];
However, after using the literal syntax, you only need to do this:
NSArray *[email protected][@“cat”,@“dog"];

For arrays, we can use the objectatindex method to obtain the content under a specified subscript through the Array Operation:
NSString *dog=[animals objectAtIndex:1];

With letaral, you can do this:
Nsstring * dog = animals [1];
Note the following when using the concise Syntax: if an object in the array is null, it will report an exception. Because the literal syntax is actually dynamically created and added to the stack, the exception you get is as follows:
The following is an example:
    id [email protected]"wildcat";    id [email protected]"Lves Li";    id [email protected]"lele";       NSArray *arrayA=[NSArray arrayWithObjects:obj1,obj2,obj3, nil];    NSArray *[email protected][obj1,obj2,obj3];

If obj2 = nil; in this case, the array arrayb defined by the concise syntax will report an exception, but arraya will not, but arraya only has a variable obj1. Because arraywithobjects: The method does not add variables until it encounters nil, and no exception is reported. This difference shows that literal is more secure. Throwing an exception makes the application crash better than creating an array with fewer variables than expected. Reporting an exception means that we can locate the problem more easily and solve the problem.

Literal dictionary
Not complete to be continued: Reprinted please indicate original from: http://blog.csdn.net/wildcatlele










Valid objective-C 2.0 Note 3 (literal syntax short syntax)

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.