New syntax learning of Objc

Source: Internet
Author: User

In this year's WWDC2012, we introduced the new Objc syntax. It's really nice to use and the code is simple and clear, just like the script language.
NSArray
Previously, an array was initialized like this.
NSArray * array = [NSArray arrayWithObjects: a, B, c, nil];

The new method is
NSArray * array = @ [a, B, c];

[Array objectAtIndex: 1] the corresponding new method is array [1].


NSDictionary
Previously, a dictionary was initialized like this.
NSDictionary * dict = [NSDictionary dictionaryWithObjects: @ [o1, o2, o3]
ForKeys: @ [k1, k2, k3];

The new method is as follows:
NSDictionary * dict =@{ k1: o1, k2: o2, k3: o3 };

Id obj = [dict objectForKey: @ "1"] the corresponding new method is id obj = dict [@ "1"]
Here @ "1" is NSString or NSNumber still to be studied.

NSNumber
The previous implementation was as follows:
NSNumber * number;
Number = [NSNumber numberWithChar: 'X'];
Number = [NSNumber numberWithInt: 12345];
Number = [NSNumber numberWithUnsignedLong: 12345ul];
Number = [NSNumber numberWithLongLong: 12345ll];
Number = [NSNumber numberWithFloat: 123.45f];
Number = [NSNumber numberWithDouble: 123.45];
Number = [NSNumber numberWithBool: YES];

The new method is as follows:
NSNumber * number;
Number = @ 'X ';
Number = @ 12345;
Number = @ 12345ul;
Number = @ 12345ll;
Number = @ 123.45f;
Number = @ 123.45;
Number = @ YES;


How does it feel? Is it getting better and better?

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.