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