Evolution of Nsarray, Nsdictionary and NSNumber in Objective-c

Source: Internet
Author: User

Starting with xcode4.4, the LLVM4.0 compiler adds some new features to Objective-c. Creating an array nsarray, a hash table nsdictionary, and a numeric object NSNumber can be as simple and convenient as the initialization of a nsstring. Mother is no longer worried about the process of writing is sour.

Interested friends can focus on LLVM compiler related documentation: http://clang.llvm.org/docs/ObjectiveCLiterals.html

Examples of nsdictionary and nsnumber from: http://cocoaheads.tumblr.com/post/17757846453/objective-c- Literals-for-nsdictionary-nsarray-and

I. Nsarray

The first is the very common nsarray,nsmutablearray. Nsarray is a static array that is initialized and fixed. If you want to insert, delete, update, and so on the elements of an array, you must use the OBJECTIVE-C dynamic array Nsmutablearray.

Before LLVM4.0, the Nsarray initialization method is as follows. Note: The following methods can continue to be used after LLVM4.0.

Initialization of Nsarray before LLVM4.0

Nsarray *oldone = [Nsarray arraywithobjects:@ "1st", @ "2nd", @ "3th", nil];

Gets the 2nd value of the array

NSString *s = [Oldone objectatindex:1];

After LLVM4.0, the Nsarray initialization method is as follows.

Nsarray *newone =@[@ "1st", @ "2nd", @ "3th"];

Gets the 2nd value of the array

NSString *s = newone[1];

Especially to say Nsmutablearray. Before LLVM4.0, if you want to update an element of an array, you typically use the following method.

Initialization of Nsmutablearray before LLVM4.0

Nsmutablearray *oldmutable = [Nsmutablearray arraywitharray:old];

[mutable replaceobjectatindex:1 withobject:@ "disposed"]; Update an element

When writing some common algorithms, the following features cause a bit of trouble writing.

/* Want to update an element of Nsmutablearray? Please initialize this element first * *

Nsmutablearray *oldmutable = [[Nsmutablearray alloc] init]];

/* Must assign an initial value to each element as follows, otherwise exception will occur

for (int h = 0; h < 5; h++) {

[Oldmutable addobject:@ "1"];

}

@try {

[mutable replaceobjectatindex:1 withobject:@ "disposed"];

}

@catch (NSException *exception) {

NSLog (@ "%@", [exception description]);

}

This LLVM4.0 simplifies this process and can be done in the following way.

After LLVM4.0

Nsmutablearray *newmutable = [Nsmutablearray alloc] init];

NEWMUTABLE[2] = @ "MyObject";

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.