Today, to sort out the contents of objective-c literals, the literals translates into literal values or literals, which are directly written to the source code. This is not an unfamiliar concept, we can often see when writing programs. We've seen NSString's literal in a long time:
@" Hello World ";
Its syntax is very simple, the above code is to create a NSString object greeting by preceding the C string with the @ symbol, the entire code looks simple and easy to understand, if there is no direct volume syntax, then creating this greeting may use the following method:
1 nsstring *greeting = [NSString stringwithutf8string:"HelloWorld"];
After Xcode4.4 and LLVM4.0, some new features were proposed, one of which was to use literal syntax for NSNumber, Nsarray, and Nsdictionary.
NSNumber
Before the new feature is presented, one of the NSNumber objects we're going to create might be this:
1 nsnumber *anumber = [NSNumber numberwithint:];
So now it can be written like this:
1 nsnumber *anumber = @;
Of course here is not only for-numberwithint: applicable, but also for others:
1 //without the literal way:2NSNumber *abool =[NSNumber Numberwithbool:yes];3NSNumber *achar = [NSNumber Numberwithchar:'a'];4NSNumber *aunsigned = [NSNumber numberwithunsignedint:126];5NSNumber *along = [NSNumber numberwithlong:27L];6NSNumber *afloat = [NSNumber numberwithfloat:3.14F];7NSNumber *adouble = [NSNumber numberwithdouble:3.14];8 9 //the wording of the literalTenNSNumber *abool =@YES; OneNSNumber *achar = @'a'; ANSNumber *aunsigned = @22U; -NSNumber *along = @27L -NSNumber *afloat = @3.14F; theNSNumber *adouble = @3.14;
In addition to this, you can also use an expression:
1 nsnumber *result = @ (2+3*4);
NS Array
When you do not use literal syntax, creating an array is usually the case:
1 nsarray *aarray = [Nsarray arraywithobjects: [nsnumber numberwithint:@ "Kuntzuo "@"Hello", nil];
With the literal syntax, you can write this:
1 nsarray *aarray = @[@@ "kuntzuo"@"Hello "];
The way to create the literal of an array becomes more concise, preceded by a literal @ symbol in front of the square brackets, where the contents are written, and no longer requires nil as the end sign, and if nil is omitted, a warning will be given.
nsdictionary
The traditional way to create a nsdictionary is this:
1Nsarray *keys = [Nsarray arraywithobjects:@"Name",@"Sex",@" Age", nil];2 3Nsarray *objects = [Nsarray arraywithobjects:@"Kuntzuo",@"male", [NSNumber Numberwithint: A], nil];4 5Nsdictionary *myprofile =[Nsdictionary dictionarywithobjects:objects Forkeys:keys];6 7 //or a8 9Nsdictionary *myprofile =[nsdictionary Dictionarywithobjectsandkeys:Ten One @"Kuntzuo",@"Name", A - @"male",@"Sex", - the[NSNumber Numberwithint: A],@" Age", - -NIL];
Both of these methods seem to be easy and confusing. The literal syntax gives us a better option to look at the following wording:
1 nsdictionary *myprofile = @{"Name":"kuntzuo", "Sex":" male ","Age": @ ;
In addition to using literal to create Nsarray, NSNumber, Nsdictionary. If the collection is a mutable type such as
Nsmutablearray, Nsmutabledictionary, the new feature also allows you to access and modify the values in the collection in a subscript manner. The following code demonstrates this feature in a nutshell.
1 //for the above Nsarray *aarray2Nsarray *aarray = @[@ A,@"Kuntzuo",@"Hello"];3Nsmutablearray *mutablearray = [@[@ A,@"Kuntzuo",@"Hello"] mutablecopy];4 5 //Traditional access Methods6NSString *greeting = [Mutablearray objectatindex:2];//"Hello"7 //New Method Access8NSString *greeting = mutablearray[2];//"Hello"9 //New Method ModificationTenmutablearray[2] =@"Hello World"; One A - //for the above nsdictionary -Nsdictionary *myprofile = @{"Name":"Kuntzuo","Sex":"male"," Age":@ A} theNsmutabledictionary *mutabledic =[Myprofile mutablecopy]; - //Traditional access Methods -NSString *name = [Mutabledic objectforkey:@"Name"];//"Kuntzuo" - //New Method Access +NSString *myname = mutabledic[@"Name"];//"Kuntzuo" - //New Method Modification +mutabledic[@"Name"] =@"Kuntian Zuo";
That's all I know about literals, and even now it's not new, but it's still very necessary for us to be able to write code, and it can be more readable and better-styled.
Objective-c's literals (literal)