Use of literal syntax--ios

Source: Internet
Author: User

When using OC, nsstring,nsnumber,nsarray,nsdictionary is often used, and the following is the use of their literal syntax.

(1) Literal value

It is sometimes necessary to block integers, floating-point numbers, and Boolean values into OC objects

General wording:

NSNumber *num = [NSNumber numberwithint:1];

Use literal syntax:

NSNumber *num = @1;

Other types use literal syntax:

NSNumber *[email protected];

NSNumber *[email protected];

NSNumber *[email protected];

NSNumber *[email protected];

NSNumber *[email protected] ' a ';

The literal also applies to the following expression:

int x=5;

Float y =6.15f;

NSNumber *[email protected] (x*y);

(2) Literal array

General wording:

Nsarray *animals=[nsarray arraywithobject:@ "cat", @ "dog", @ "mouse", nil];

Use literal syntax:

Nsarray *[email protected][@ "cat", @ "dog", @ "Mouse"];

Operation of the array

General wording:

NSString *dog=[animals Objectatindex:1];

Use literal:

NSString *dog=animals[1];

(3) Literal dictionary

General wording:

Nsdictionary *persondata=[nsdictionarydictionarywithobjectsandkeys:@ "Matt" @ "FirstName", @ "Galloway", @ "LastName" , [NSNumber numberwithint:28],@ "age", nil];

Use literal:

Nsdictionary *[email protected]{@ "firstName": @ "Matt" @ "LastName": @ "Galloway" @ "Age": @28};

Operation of the Dictionary

General wording:

NSString *lastname=[persondata objectforkey:@ "LastName"];

Use literal:

NSString *lastname=persondata[@ "LastName"];

(4) Variable groups and dictionaries

By removing the label operation, you can access an element in the array that corresponds to a key in the index or dictionary. If the array and the Dictionary object are mutable, the element values can also be modified by subscript.

General wording:

[Mutablearray replaceobjectatindex:1 withobject:@ "Dog"];

[Mutabledictionary setobject:@ "Galloway" forkey:@ "LastName"];

Use literal:

Mutablearray[1][email protected] "dog";

mutabledictionary[@ "LastName"][email protected] "Galloway";



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Use of literal syntax--ios

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.