Use literal syntax to make iOS code more beautiful

Source: Internet
Author: User

literal syntax
First, literal value
Complex methods:
NSNumber *somenumber=[nsnumber numberwithdouble:3.4];
NSLog (@ "The value is%@", somenumber);
Alternative methods:
NSNumber *[email protected];
NSNumber *[email protected];
NSLog (@ "The value is%@", a);

NSLog (@ "The value is%@", b);


Second, literal array
Complex methods:
Nsarray *arr=[nsarray arraywithobjects:@ "Hello" @ "Richard" @ "Yang", nil];
NSLog (@ "The first object is%@", [arr objectatindex:0]);
Alternative methods
Nsarray *[email protected][@ "Hello" @ "Richard" @ "Yang"];
NSLog (@ "The first object is%@", arr1[1]);
Precautions:

When creating an array with literal syntax, an exception is thrown if the element object is nil, and the data in front of nil is created with arraywithobjects.

Third, literal dictionary
Complex methods:
nsdictionary *persondic=[nsdictionary dictionarywithobjectsandkeys:@ "Richard", @ "name", @ "001", @ "num", nil ];
NSLog (@ "name is%@", [persondic valueforkey:@ "name
Alternative methods:
Nsdictionary *[email protected]{@ "name": @ "Richard" @ "num": @ "001"};

NSLog (@ "The name is%@", persondic[@ "name"]);


Iv. Common mutable objects
Nsmutablearray *arr1=[@[@ "Hello" @ "Richard" @ "Yang"] mutablecopy];

Variable objects created with literal syntax need to be added with Mutalecopy


Fifth, use the literal syntax to modify the value
Nsmutablearray *arr1=[@[@ "Hello" @ "Richard" @ "Yang"] mutablecopy];
NSLog (@ "The first value is%@", arr1[0]);
Arr1[0][email protected] "Andy";

NSLog (@ "The first value is%@", arr1[0]);


Vi. Summary
1, using the literal syntax to create objects, concise and to
2. Accessing the dictionary by removing the label operation to access the array and take the key operation
3. When creating an array or dictionary with literal syntax, if there is nil in the value, the exception will be thrown



Use literal syntax to make iOS code more beautiful

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.