OC-arrays, dictionaries, collections

Source: Internet
Author: User
Tags string format

An array is an ordered set, and an array in OC can only store object types, and there is no limit to the type of object.

Nsarray: Immutable groups: When an object is created, the number of elements in the array cannot be changed

Nssmutablearray: mutable array: Once an object is created, the elements in the array can be manipulated flexibly

Nsarray *names = [[Nsarray alloc] initwithobjects:@ "Frank", @123, @ "monkey", nil];

Note: Nil as an array holds the end flag of an element, and multiple elements are separated by commas.

Common methods of Nsarray

1. Number of > elements

@property (readonly) Nsuinteger count;

2. > Gets the element that specifies the subscript in the array

-(ID) Objectatindex: (Nsuinteger) index;

3. > is used to determine if a given object is contained in an array

-(BOOL) Containobject: (ID) anobject;

4.> gets the index of the array element

-(Nsuinteger) Indexofobject: (ID) anobject;

5. > intercepts a given string and puts the truncated multi-segment string into the array

-(Nsarray *) componentsseparatedbystring: (NSString *) separator;

6. > Stitching elements in an array into a complete string object in the given string format

-(NSString *) componentsjoinedbystring: (NSString *) separator;

Variable group Nsmutablearray

1. Add an object to the > array

-(void) AddObject: (ID) object;

2. Insert an object at a specified position in the > array

-(void) InsertObject: (ID) anobject atindex: (nsuinteger) index;

3. Remove an object from the > array

-(void) Removeobject: (ID) object;

4. > Move the last object in the divisor group

-(void) removelastobject;

5. > Remove all elements from the divisor group
-(void) removeallobjects;

6. Remove elements from a specified position in the > array

-(void) Removeobjectatindex: (Nsuinteger) index;

7. > Replace the object at the specified position with the specified object

-(void) Reolaceobjectatindex: (nsuinteger) index Withobject: (ID) anobject;

8.> interchange the specified two subscript corresponding objects

-(void) Exchangeobjectatindex: (Nsuinteger) idx1 Withobjectatindex: (Nsuinteger) inx2;

Dictionary: A dictionary is an unordered collection used to store data with one by one correspondence.

Each object stored in the dictionary is a key-value pair, with a key-value pair containing two parts of key and value, and the value of key and value are object types.

For each pair of Key-value is called an entry (Entry).

Unlike arrays, dictionaries access elements by key

Key and value must both be object types

The storage of key-value pairs in a dictionary is unordered

The dictionary is divided into two types of immutable dictionaries (nsdictionary), which look outside the dictionary (nsmutabledictionary).

1. > Get the number of key-value pairs in the dictionary

@property (readonly) Nsuinteger count;

2. > Get all the keys in the dictionary

@property (readonly, copy) Nsarray *allkeys;

3. > Get all the values in the dictionary

@property (readonly, copy) Nsarray *allvalues;

4. > To get the corresponding value according to the key

-(ID) Objectforkey: (NSString *) Anattribute;

5. Add a new key-value pair to the > dictionary and modify a key-value pair

-(void) SetObject: (ID) anobject forkey: (id<nscopying>) Akey;

6. > Remove the key value pair corresponding to the specified key
-(void) Removeobjectforkey: (ID) Akey;

7.> Remove all key-value pairs from the dictionary

-(void) removeallobjects;

Collections: Collections are used in the development process and do not have arrays and dictionaries frequently. The set of OC is divided into immutable set (Nsset) and mutable set (Nsmutableset).

Features of the collection:

No two identical objects can exist in a cross-specific set

The objects in the unordered collection are not in the order, and the objects that are added first are not necessarily in the first position in the collection, and are not necessarily in the last position

Collections are often used to deal with reuse problems
Initialize method

Nsset *name = [[Nsset alloc] initwithobjects:@ "Frank", @ "Duck", @ "monkey", nil];

Note: Once immutable collections are created, objects in the collection cannot be modified, only objects can be read from the collection, and there is no quick way to create the literal of the collection object.

1. > Get the number of objects in the collection

@property (readonly) Nsuinteger count;

2. > Get all the objects in the collection

@property (readonly, copy) Nsarray *allobjects;

3. > Remove an object from the collection

-(ID) anyobject;

4. > Determines whether a given object is contained in the collection

-(BOOL) Containobject: (ID) anobject;

The parent class of the Nsmutableset (mutable collection) is Nsset, which has all the nsset methods, and adds the actions of the Add and delete objects on top of it.

1. > Add an Object

-(void) AddObject: (ID) object;

2. > Removing an Object

-(void) Removobject: (ID) boject;

3. > Remove All Objects

-(void) removeallobjects;

Three big containers in OC: arrays, dictionaries, collections, three containers total storage is object type

Array: An ordered set of objects that need to be used when we need to manage an ordered set
Dictionary: is an unordered collection, the object stored inside is a key value pair, we need to use the corresponding key to manipulate the data

The collection of collections is an unordered container, and objects in the container cannot be duplicated

The variable container class object is a subclass of immutable container class object, and it expands the operation of adding and deleting the original object on the basis of owning the function of parent class.

OC-arrays, dictionaries, collections

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.