Simple use and nsarray of OC arrays

Source: Internet
Author: User

As in the previous article, the importance of the array is self-evident, in the process of OC programming we will continue to use Nsarray, and C language is different, we here can only save the OC object type, cannot save the C language Basic data type, can not save nsnull type, But our array here can be a variety of object types, for example, in the same arrays, can be stored in the string type, but also can save the object of the future integer type, or even another array, you can also save other object types (the C data type to OC object type will be described below), However, the same data type is stored in the same array for ease of use. At the end of the OC array with nil, if you save a nil data in the middle of the array, then the data behind nil is not readable.

Like NSString, arrays can be divided into variable arrays (Nsmutablearray) and non-variable groups (Nsarray), and first, we start with the immutable array, because mutable arrays simply add some new methods to the immutable groups.

I. Non-variable group

1. Creation of immutable variable groups

2. Number of objects accessing an array you can use the Count property to recall that the length of the string is accessed in the string we are using the long property.

3. Get the index value of an object using the method: Indexofobject

4. Get an object for an index value how to use: Objectatindex

5. Determine if an array contains an object how to use: Containsobject

6. Get the first element of the array: arr[0]

[Arr objectatindex:0]

[Arr Firstobject]

7. Get the last element of the array: [arr Lastobject]

Second, variable array

  Variable groups are similar to immutable strings in relation to immutable groups and variable strings, and all add methods and properties, the most important of which is increment, delete, change, insert.

1. Creating a mutable array

2. Add an object to the variable array using the method: AddObject

3. Delete objects inside a mutable array:

[removeobject:@ "Xianggang"];

[Ar removeobject:@ "BJ"];

[Ar removeobjectatindex:1];

[Ar removeallobjects];

4. Replace an object inside the array

[Ar exchangeobjectatindex:0 withobjectatindex:1];

5. Inserting an Object

[Ar insertobject:@ "BJ" atindex:1];

Three, encapsulation (that is, type conversion)

1. Encapsulate C-language basic data types as OC object types we use NSNumber;

NSNumber *number = [NSNumber numberwithint:20];

NSNumber *number1 = [NSNumber numberwithfloat:20.0];

2. Encapsulates the structure and enumeration of the C language with the OC object type requiring the use of nsvalue;

struct cgrect{

Cgpoint Point;

Cgsize size;

};

struct Cgpoint {

CGFloat x;

CGFloat y;

};

struct Cgsize {

CGFloat width;

CGFloat height;

};

*/

CGRect rect = cgrectmake (100, 100, 200, 150);

Nsvalue *rectvalue = [Nsvalue valuewithrect:rect];

  

Simple use and nsarray of OC arrays

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.