IOS summary_simplified syntax for IOS development

Source: Internet
Author: User

IOS summary_simplified syntax for IOS development

The following simplified method is the new method after IOS6.0


NSNumber

// Simplify the preceding statement:

NSNumber * value1;

Value1 = [NSNumbernumberWithInt: 12345];

Value1 = [NSNumbernumberWithFloat: 123.45f];

Value1 = [NSNumbernumberWithDouble: 123.45];

Value1 = [NSNumbernumberWithBool: YES];

// Simplified statement:

NSNumber * value2;

Value2 = @ 12345;

Value2 = @ 123.45f;

Value2 = @ 123.45;

Value2 = @ YES;

// The packing expression can also be written in a similar way:

NSNumber * piOverSixteen1 = [NSNumbernumberWithDouble: (M_PI/16)];

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

// Can be abbreviated:

NSNumber * piOverSixteen2 = @ (M_PI/16 );

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

// For string expressions, note that the expression value must not be NULL; otherwise, an exception is thrown.


NSArray

// For NSArray initialization, there are a lot of writing methods, which will not be listed here. Let's look at the new writing method directly.

NSArray * array;

Array = @ []; // empty array

Array = @ [a]; // an array of Objects

Array = @ [a, B, c]; // array of multiple objects

// Compiler processing:

Array = @ [a, B, c];

// The code generated by the compiler:

Id objects [] = {a, B, c };

NSUInteger count = sizeof (objects)/sizeof (id );

Array = [NSArrayarrayWithObjects: objects count: count];

// Note that if the, B, and c objects have nil, an exception will be thrown during running. This is different from the original processing method. Be careful when coding.


NSDictionary

// Similarly, there are many initialization methods for the data structure of the dictionary. Let's look at the new method:

NSDictionary * dict;

Dict =@{}; // empty dictionary

Dict =@{@ "key1": @ "value1"}; // dictionary containing a key-Value Pair

Dict = @ {@ "key1": @ "value1", @ "key2": @ "value", @ "key3": @ "value3 "}; // dictionary containing multiple key-value pairs

// The containers built using the preceding method are immutable. To generate a variable container, You can transmit 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];



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.