iOS Summary _ios Development section Syntax simplified notation

Source: Internet
Author: User

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


NSNumber

//simplification of the previous wording:

NSNumber *value1;

value1 = [nsnumbernumberwithint:12345];

value1 = [nsnumbernumberwithfloat:123.45f];

value1 = [nsnumbernumberwithdouble:123.45];

value1 = [nsnumbernumberwithbool:YES];

//Simplified wording:

NSNumber *value2;

value2 =@12345;

value2 =@123.45f;

value2 =@123.45;

value2 =@YES;

the//boxing expression can also be written in a similar way:

nsnumber *pioversixteen1 = [nsnumbernumberwithdouble: ( m_pi / +)];

nsstring *path1 = [nsstringstringwithutf8string: getenv( "PATH")];

//Can be abbreviated as:

nsnumber *pioversixteen2 = @ ( m_pi / +);

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 look at the new wording

Nsarray *array;

Array =@[]; //Empty array

Array =@[ a ]; //An array of objects

Array =@[ A, B, C ]; //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 = [nsarrayarraywithobjects: 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

Also, 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"}; //Dictionary containing a key-value pair

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

//The container constructed using the above notation is immutable, and if you need to generate a mutable container, you can pass the-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 Summary _ios Development section Syntax simplified notation

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.