Article 3. Use more literal syntax and less equivalent methods.

Source: Internet
Author: User

Article 3. Use more literal syntax and less equivalent methods.

First, when we create an NSString object, Objective-C provides a very simple way to create an NSString. This is the string literal. Syntax:

NSString *s = @"This is a test";

If this is not the case, we need to create alloc and init.

Certificate -------------------------------------------------------------------------------------------------------------------------------------------

There is also NSNumber, which is used to store integer floating point numbers. We may use:

NSNumber *number = [NSNumber numberWithInt:1];

Now we can directly:

NSNumber *number = @1;

Certificate -------------------------------------------------------------------------------------------------------------------------------------------

NSArray, we may create an array like this:

NSArray *arr = [NSArray arrayWithObjects:@"112",@"2323",@"str",nil];

When we use a literal array:

NSArray *animals = @[@"cat",@"dog",@"pig"];

In addition, data retrieval is relatively simple. We used to take the data as follows:

NSString *s = [arr objectAtIndex:1];

When we use a literal array, we only need:

NSString *s = animals[1];

It is more convenient. Note: If the element is nil, an exception is thrown.

In fact, his effect is equivalent to creating an array first, and then adding the elements in the brackets.

In fact, literal variables are just a syntax sugar (also a sugar-coated Syntax: it refers to the syntax equivalent to another set of syntax but more convenient to use). Below is an interesting question:

For example, there are three data types:

Id obj1 =

Id obj2 =

Id obj3 =

If we use the regular method

NSArray *arr = [NSArray arrayWithObjects:obj1,obj2,obj3,nil];

Then use the literal array:

NSArray *arr= @[obj1,obj2,obj3];

In this case, if obj2 is empty and the other two are not empty, only obj1 is in the array for the method we create normally, and an error is returned for the literal array. Generally, only obj1 is created because the arrayWithOObjects method processes parameters in sequence until nil is found. So obj2 is the nil function and the execution is complete.

Certificate -------------------------------------------------------------------------------------------------------------------------------------------

Literal dictionary

The traditional creation method may be:

NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"Scott",@"name",@"boy",@"gender",@"beijing",@"address",nil];

If you use a literal dictionary, the creation method is as follows:

NSdictionary *dic = @{@"name":@"Scott",@"gender":@"boy",@"address":@"beijing"};

Like an array, once the value is nil, an exception is thrown.

Similarly, the original method is:

NSString *gender = [dic objectForKey:@"gender"];

Now we only need:

NSString *gender = dic[@"gender"];

Of course, you can also take the literal dictionary value. If it is a variable array or dictionary, you can use a subscript to change it:

mutalbeArr[1] = @"chicken";mutableDic[@"name"] = @"Scott Zhang";

Let's briefly introduce so many literal variables.

Certificate -------------------------------------------------------------------------------------------------------------------------------------------

Summary:

① When the value is nil in the dictionary or array, an exception is thrown. Make sure that the value does not contain nil.

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.