iOS Efficient Development-----literals

Source: Internet
Author: User

Objective-c (abbreviated to OC) is a complex syntax, but from the OC 1.0 version, there is a very simple way to create a NSString object, that is, the literal, that is, the core of this article. Its syntax is as follows:

Literal string

NSString * string = @ "I ' m a string";

If this method is not used, the common alloc and the Init method are necessary.

Literal value

The class provided by OC for handling numeric values (integers, floats, bool values) is NSNumber, which provides the following main class methods:

If not in literal form, create the following:

NSNumber * Number = [NSNumber numberwithint:2];

The literal way to create:

NSNumber * intnumber    = @1; NSNumber * Floatnumber  = @1.2f; NSNumber * Doublenunber = @3.14159; NSNumber * Boolnumber   = @YES; NSNumber * CharNumber   = @ ' a ';

Literal array

Arrays are commonly used data structures and are used in the following ways:

Use System method to create Nsarray * array = [Nsarray arraywithobjects:@ "a", @ "B", @ "C", @ "D", nil];//value nsstring * str = [array objectatindex : 0];//uses literal creation Nsarray * Anotherarray = @[@ "A", @ "B", @ "C", @ "D"];//value NSString * anotherstr = anotherarray[0];

Creating an array with literal literals is more secure, such as modifying the above array slightly, as follows:

Use System method to create Nsarray * array = [Nsarray arraywithobjects:@ "a", @ "B", nil,@ "D", nil];//no error: can insert Nil,nil as end flag, at this time @ "D" cannot be inserted into the array;//value NSString * str = [array objectatindex:0];//using literal creation nsarray * Anotherarray = @[@ "A", @ "B", nil,@ "D"];// Error because literal array cannot insert nil;//value NSString * anotherstr = anotherarray[0];

  

The use of literal syntax is more secure, and throwing an exception causes the application to terminate execution, which is better than finding the element less after the array is created.

Literal dictionary

OC is often used in dictionaries, OC Create a dictionary is very puzzling, writing is difficult to understand, OC is created as follows:

System commonly used to create the way, the value in front, the key after the readability is not strong nsdictionary * dic        = [nsdictionary dictionarywithobjectsandkeys:@ "Dog", @ "Animal", @ "Boy", @ "Kid", @ "bread", @ "food", nil];//take dognsstring     * dog        = [dic objectforkey:@ "Animal"];//use literal syntax to create, key in front, Value behind, simple and easy to understand nsdictionary * Anotherdic = @{@ "animal": @ "dog" @ "Kid": @ "Boy" @ "food": @ "Bread"};//value nsstring     * Anotherdog = anotherdic[@ "Animal"];

Advantages of using literals:

1, the object created by the literal, compared with the traditional way of creation, more concise.

2. You can access the elements corresponding to the keys in the array or dictionary by going to the small label operation.

3, create an array with literal, if the element is nil, will prompt error.

Disadvantages:

In addition to literals, the objects created must belong to the foundation framework, and if they are subclasses of these classes, they cannot be created with literal syntax.

  

iOS Efficient Development-----literals

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.