IOS 6 new shortcut initialization notation

Source: Internet
Author: User

Ext.: http://www.2cto.com/kf/201408/328974.html

The following simplified notation is a new formulation after IOS6.0

NSNumber

To simplify the previous wording:

NSNumber *value1;

value1 = [NSNumber numberwithint:12345];

value1 = [NSNumber numberwithfloat:123.45f];

value1 = [NSNumber numberwithdouble:123.45];

value1 = [NSNumber numberwithbool:yes];

The simplified wording:

NSNumber *value2;

value2 = @12345;

value2 = @123.45f;

value2 = @123.45;

value2 = @YES;

Boxing expressions can also be written in similar notation:

NSNumber *pioversixteen1 = [NSNumber numberwithdouble: (M_PI/16)];

NSString *path1 = [NSString stringwithutf8string:getenv ("PATH")];

can be abbreviated as:

NSNumber *pioversixteen2 = @ (M_PI/16);

NSString *path2= @ (getenv ("PATH"));

For string expressions, it is important to note that the value of an expression must not be null, or an exception will be thrown.

Nsarray

For Nsarray initialization, there are a lot of writing, here is no longer one by one listed, we directly see the new wording

Nsarray *array;

Array = @[]; Empty array

Array = @[A]; An array of objects

Array = @[A, b, c]; An array of multiple objects

When the compiler is processing:

Array = @[A, b, c];

Compiler-Generated code:

ID objects[] = {A, b, c};

Nsuinteger count = sizeof (objects)/sizeof (ID);

Array = [Nsarray arraywithobjects:objects count:count];

Note that if the A,b,c object has nil, the runtime throws an exception, which is different from the original processing, so be careful when coding.

Nsdictionary

Similarly, for a dictionary of this data structure, there are many ways to initialize, we look at the new wording:

Nsdictionary *dict;

Dict = @{}; Empty dictionary

Dict = @{@ "Key1": @ "value1"}; A dictionary that contains a key-value pair

Dict = @{@ "Key1": @ "value1", @ "Key2": @ "value" @ "Key3": @ "Value3"}; Dictionary with multiple key-value pairs

Containers built using the above notation are immutable, and if you need to generate a mutable container, you can pass a-mutablecopy message. For example

Nsmutablearray *mutableplanets = [@[

@ "Mercury", @ "Venus", @ "Earth",

@ "Mars", @ "Jupiter", @ "Saturn",

@ "Uranus", @ "Neptune"

] Mutablecopy];

Nsmutabledictionary *dic=[@{@ "Key1": @ "value1", @ "Key2": @ "value" @ "Key3": @ "Value3"} Mutablecopy];

IOS 6 new shortcut initialization notation

Related Article

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.