Knowledge points
the focus function of the 1.NSArray class is used
The key function of the 2.NSMutableArray class uses 3. String segmentation and splicing
============================
Nsarray, Nsmutablearray
1. "Knowledge that needs to be understood"
1.1 Array in C language
1, the same data type collection;
int a[10];
float B[20];
Char c[30];
2, the array address is continuous;
1. The difference between OC array and C array
1, Nsarray is a class array is an object, storage is also an object;
2. Arrays cannot directly store basic data types: char, int, shot, long ...
3, the array is divided into two kinds: variable array, non-variable group, Nsarray non-variable group class, Nsmutablearray is variable array class;
Difference:
The base data type is stored in the C array;
The object is stored in the OC array;
2. "Knowledge to Remember"
Class name of the immutable group: Nsarray
2. How to create an array
2.1.
To create by object:
Nsarray *arr1 = [[Nsarray alloc] initwithobjects:@ "string1", @ "string2", @ "String3", nil];
Created by an array of
Nsarray *ARR2 = [[Nsarray alloc] initwitharray:arr1];
Created by the method of the class
To create by object:
Nsarray *ARR3 = [Nsarray arraywithobjects:@ "String 1", @ "String 2", @ "String 3", nil];
Created by an array of
Nsarray *ARR4 = [Nsarray arraywitharray:arr2];
2.2. New features of OBJECTIVE-C 2.0
Since Xcode4.6, arrays are allowed to be created in a manner similar to C arrays, only for non-variable groups
@[Object 1, Object 2, Object 3 ...];
Nsarray *ARR5 = @[@ "string1", @ "string2", @ "String3"];
2.4. Create an empty object
NSNull *nilobject = [NSNull null];
==========================================================
3. What are the elements of an array
1) An array element can be any object
Exercise: 1. Create an array with 3 string objects in three ways
===========================
Nsarray Common methods
1. Get the number of array elements
Nsuinteger count = [array count];
2, get the corresponding element by index number (2 kinds) (important) nsstring *s = [array objectatindex:0];
NSString *s1 = array[1];
3. Remove the last element
NSLog (@ "%@", [array lastobject]);
4. Get the subscript in the array by object
NSLog (@ "%li", [array indexofobject:@ "string1"]);
5. The array contains elements in the array anobject
BOOL ret = [array containsobject:@ "String4"];
===================================
5. How to remove part of an array element sub****
1) Subarraywithrange Message
Function: Removes the element from the range specified in the array
6-1. Get the Sub-array (part of the array element) according to the range;
Nsrange range = Nsmakerange (1, 3);
Nsarray *subarray = [array subarraywithrange:range];
NSLog (@ "%@", Subarray);
2) objectsatindexes Message
Function: Gets the element of the specified subscript from the array
6-2, according to the subscript to get the sub-array:
An integer collection object;
Nsmutableindexset *indexset = [[Nsmutableindexset alloc] init];
[Indexset addindex:0];
[Indexset Addindex:2];
[Indexset Addindex:4];
Nsarray *subarray2 = [array objectsatindexes:indexset];
Practice:
1. Design a human, methods have speak speak, create a person object P1;
Design a dog class, methods have bark called, create a dog object D1;
Create an array that stores a string object, a person object, a dog object;
The type of the elements within the array is judged by the loop,
If it is human, call method speak;
If it is a dog, call the method bark;
If it is a string, direct output;
2. Create an array, nsarray *arr = @[@ "1", @ "2", @ "3", @ "4", @ "5", @ "6", @ "7", @ "8", @ "9", @ "10"];
Take out the elements in the array that are divisible by 3 to form a new array and output;
===========================
Traversal of the Nsarray array
Recall: How to iterate through all the elements of an array in the C language
1. Iterate through the array by subscript
for (Nsinteger i = 0; i < [arr count]; i++) {
NSLog (@ "%@", Arr[i]);
}
2. Nsenumerator Enumerator
1) objectenumerator Message
Function: Positive sequence output all elements
Nsenumerator *enumertor = [arr objectenumerator];
2) Reverseobjectenumerator Message
Function: Output all elements in reverse order
Nsenumerator *revertor = [arr reverseobjectenumerator];
3. Fast Enumeration method
1) for in syntax
for (NSString *s in arr) {//Take the ARR array inside each object one by one;
NSLog (@ "%@", s);
}
Exercise: 1. Find the longest string in the array
2. Find the string with the shortest length in the array
Summary of the array:
1, an array is also an object (the Nsarray class creates an array object)
2, immutable array once created, the number of elements and elements of the element and the order of elements can not be changed
3, an array of elements, can be any type of object, not simply can be a string, including array objects, human objects, dog objects .....
4, the element stored in the array is the address of the object
5, the array can be printed with NSLog (%@), and each element of the group is printed
=====================================
Nsmutablearray and Nsarray (the Nsarray method described above can be used in Nsmutablearray objects)
Nsmutablearray inherit Nsarray;
1.NSMutableArray the role of the storage object; (Increased deletion)
2. How to create a Nsmutablearray
1) Create with arraywithcapacity
is a reference capacity, the array will change the length according to the actual element
2) Create with Alloc
============================
Nsmutablearray (increased deletion)
1. Adding array elements
adding elements
[Arrm addobject:@ "Lixiaolong"];
Add an object from an array
[Arrm addobjectsfromarray:@[@ "Zhangsan", @ "Lisi", @ "Laowang"];
insert****
1) How to insert an element
[Arrm insertobject:@ "Zhizhuxia" atindex:2];
2) inserting multiple elements
Nsmutableindexset *index = [[Nsmutableindexset alloc] init];//empty Integer Collection Object
[Index addindex:0];
[Index Addindex:2];
[Index Addindex:3];
[Arrm insertobjects:@[@ "one", @ "one", @ "three"] atindexes:index];
The number of objects and the whole number of numbers should be consistent and equal;
2. How to delete an element remove*****
1) Delete the element with the specified subscript
[Arrm removeobjectatindex:1];
2) Delete last element
[Arrm Removelastobject];
3) Delete elements according to the specified object
[Arrm removeobject:@ "Lisi"];
4) Delete the specified element in a certain range
[Arrm removeobject:@ "Zhangsan" Inrange:nsmakerange (0, 4)];
5) Delete all elements of the specified range:
[Arrm Removeobjectsinrange:nsmakerange (3, 2)];
6) Deletes the specified element according to an array
[Arrm removeobjectsinarray:@[@ "1", @ "2", @ "3", @ "one"];
7) Delete all specified subscript elements at once
Nsmutableindexset *index
[Arrm Removeobjectsatindexes:index];
8) Delete all elements
[Arrm removeallobjects];
Summarize:
Add: add*****, insert*****
Deletion: remove*****
Practice:
1. Create a mutable array that contains a number of different strings that require the specified string to be deleted
2. Create an array that contains a number of different strings that require that all elements of the subscript divisible by 3 be deleted
3. First define a person class, containing a member variable: No, randomly generated no count
If the subscript of a person in the array is divisible by a count, the dequeue (remove)
Arc4random (); Generate random numbers;
5. Swap the two elements in the array: Exchangeobjectatindex:withobjectatindex
[Arrm exchangeobjectatindex:0 Withobjectatindex:2];
Practice:
1. Save the elements in the array in reverse order
@[@ "1", @ "2", @ "3", @ "4", @ "5"]
@[@ "5", @ "4", @ "3", @ "2", @ "1"];
6. Replacing elements
1) 1. Replace an element replaceobjectatindex:withobject:
[Arrm replaceobjectatindex:0 withobject:@ "three"];
2. Replace multiple elements replaceobjectsatindexes:withobjects:
[Arrm replaceobjectsatindexes:index withobjects:@[@ "four" @ "one"];
3. SetArray: All Modifications
[Arrm setarray:@[@ "Fire"];
Summarize:
Add: add****, insert*****
Deletion: remove******
Change: replace****, exchange****, set***** (all changed to ...)
===========================
Segmentation and splicing of strings
1. How to split the string "+" "hello_world+ddd"
1) componentsseparatedbystring Message
Function: Splits a string according to a specified string
2) Componentsseparatedbycharactersinset Message
Function: Use the specified Nscharaterset collection to split the string
Note: If the split condition appears at the beginning or the end, an empty string @ "" will appear
The difference between the use of 2.NSCharacterSet sets and Nsindexset
Requirement: Now you need to divide a string by number
1) Nscharacterset effect
Nscharacterset is an unordered character set,
Used primarily to determine whether a known string contains a character set, and not a string to be saved
2) charactersetwithcharactersinstring Message
Function: Constructs a Nscharaterset collection based on the characters in the specified string
3. String concatenation
1) componentsjoinedbystring Message
Function: Connects each element with a specified string
Example: time, sentence ...
Practice:
The string @ "I am a Man" and @ "You is so good";
Combination of @ "I am is a so man good"
===========================
The role of NSNumber
Requirements: An array of OC cannot be placed into the basic data type of the C language
Function: Convert the basic data type of C to NSNumber object
=============================
8, Nsmutablearray array sorting
1) Array Sorting method
Sort by ASC code size
Provides a sorting method that automatically sorts the array according to the rules of the sorting method
Nsarray *arr1 = [arr sortedarrayusingselector: @selector (compare:)];
============================
Homework:
1. Sort the array according to the length of the string (Bubbling Ascending) (select Descending)
2: Create an array, the element is the object address of 5 dog class, iterate the array, is each element executes bark function
Bark function can print out the name and age of dog
3: Create an array, there are three cat objects in the array, there are three dog objects, three ways to traverse the array, if it is a cat object to send a wash message, print "Kitten in the Face", if it is a dog object, send bark message to the object, print the dog cry
4: Create a dog class, rewrite the description method of the class, print out a word
Objective-c Arrays and mutable arrays