Array in Oc, Oc Array

Source: Internet
Author: User

Array in Oc, Oc Array

======================================

Array

======================================

I. Recognize Arrays

In oc, NSObject object subclasses can be placed in the array collection, but basic data types such as int, float, and double must be converted before they can be saved to the array.

Arrays in oc start with NS and can be divided into variable arrays and immutable arrays;

1. Immutable array (NSArray)

After the array is initialized during creation, it cannot be added, deleted, or modified again;

// NSArray is a class, an array class encapsulated by Apple.

// The array must be allocated with memory before use,

// InitWithObjects word-by-word translation: init -- initialization with --- for... objects --- multiple objects

// InitWithObjects initializes the memory space allocated by [NSArray alloc] with multiple objects

NSString * str = [NSString stringWithFormat: @ "% d", 123];

NSArray * arr = [[NSArray alloc] initWithObjects: @ "one", @ "two", @ "three", nil];

// [Note] surrounded by parentheses. Each data is in an array data format separated by commas (,).

Dog * mydog = [[dog alloc] init];

Mydog. value = 100;

Cat * mycat = [[cat alloc] init];

// Arrays in oc can store different types of data

// Define the objectArr object, allocate memory to it, and initialize the value. Do not omit the end of the last nil.

// An array that cannot be added, deleted, or modified after array initialization. It is an immutable array. The data of an immutable array is assigned a value only during the first initialization, and cannot be added, deleted, or modified afterwards; it can be understood as a read-only method;

NSArray * objectArr = [[NSArray alloc] initWithObjects: mydog, mycat, @ "yudejun", nil];

NSLog (@ "% @ \ n % @", arr, objectArr );

// Retrieve Element

// [Note] array out-of-bounds prohibited

// Implicit conversion

//

Dog * tmp = [objectArr objectAtIndex: 0];

NSLog (@ "% d", tmp. value );

// Obtain the number of array elements

NSLog (@ "% ld", [objectArr count]);

// Traverse the Array

For (int I = 0; I <[arr count]; I ++ ){

NSLog (@ "% @", [arr objectAtIndex: I]);

}

// There is a simpler way to traverse arrays in oc

// This syntax is the for (in) syntax. The for loop first checks whether NSString * str is in arr. If yes, the loop continues.

// The array traverses the number of arr elements.

For (NSString * str in arr ){

NSLog (@ "% @", str );

}

// Comparison of advantages and disadvantages

// The first method is convenient for Traversing specified elements, but it is cumbersome to write.

// The second method is concise and will not cause program crash due to array out-of-bounds

// These two methods complement each other. In some cases, you must use the first method to solve the problem.

// Starting from Xcode4.6, Apple provides the following method:

// Creates an unchangeable array and initializes the value.

NSArray * TempArr = @ [@ "yu", @ "de", @ "jun"];

// The above method is equivalent: NSArray * TempArr = [[NSArray alloc] initWithObjects: @ "yu", @ "de", @ "jun", nil];

NSLog (@ "% @", TempArr );

// [Arr objectAtIndex: 0]

NSLog (@ "% @", arr [1]);

 

// NSMutableArray is a variable array and can be added, deleted, or modified.

NSMutableArray * m_arr = [[NSMutableArray alloc] init];

// Add Element

[M_arr addObject: @ "1"];

[M_arr addObject: mydog];

[M_arr addObject: @ "3"];

// Delete the element whose subscript index value is 0

[M_arr removeObjectAtIndex: 0];

// Exchange array elements

[M_arr exchangeObjectAtIndex: 0 withObjectAtIndex: 1];

// Clear the array and delete all elements in the array

[M_arr removeAllObjects];

// Delete the last element in the array element

[M_arr removeLastObject];

// Insert an element. When the index value is 0, the original element position is moved back.

[M_arr insertObject: @ "haha" atIndex: 0];

// Replace the element with the object Value 999 to replace the value of The 0th Element

[M_arr replaceObjectAtIndex: 0 withObject: @ "999"];

// Determine whether an object is included

BOOL isContain = [m_arr containsObject: @ "999"];

If (isContain ){

NSLog (@ "include ");

}

Else

{

NSLog (@ "does not contain ");

}

 

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.