Advanced syntax explaining
I. Introduction to the collection of Nsset and Nsmutableset
1) Nsset, Nsmutableset sets, elements are unordered, cannot have duplicate values.
2) Create an immutable collection object with an instance method
For example:
The macro defines a # define TOBJ (n) [NSNumber numberwithint:n]nsset *set1=[[nsset alloc]initwithobjects:tobj (2), Tobj (3), TOBJ (3), Tobj (1), Tobj (5), nil];
2) Create an immutable collection object with a class method such as:
1 Nsset *set2=[nsset setwithobjects:tobj (2), Tobj (4), Tobj (6), nil];
3) Nsset Fast Traversal method (unordered, so there is no subscript) for example:
1 for (id num in Set1) {2 NSLog (@ "%@", num); 3}
4) Setset is used to modify the contents of the collection for example: [MSet Setset:set1];
5) Intersectset is used to get the intersection of two sets (returns the same elements in two collections). For example:
#define TOBJ (n) [NSNumber numberwithint:n]nsset *set1=[[nsset alloc]initwithobjects:tobj (2), Tobj (3), TOBJ (3), TOBJ (1) , Tobj (5), nil]; Nsset *set2=[nsset setwithobjects:tobj (2), Tobj (4), Tobj (6), nil]; [MSet Intersectset:set2]; NSLog (@ "intersect:%@", MSet); Results: 2
6) Unionset is used to get the set of two sets (returns all the elements in the two collection if only one of them is shown repeatedly) for example:
1 #define TOBJ (n) [nsnumber numberwithint:n]2 nsset *set1=[[nsset alloc]initwithobjects:tobj (2), Tobj (3), TOBJ (3), TOBJ ( 1), Tobj (5), nil];3 nsset *set2=[nsset setwithobjects:tobj (2), Tobj (4), Tobj (6), nil];4 [MSet intersectset:set2];5 NSLog ( @ "intersect:%@", MSet); Results: 123456
7) Minusset is used to get the difference of two sets for example:
1 #define TOBJ (n) [NSNumber numberwithint:n]2 nsset * mSet =[[nsset alloc]initwithobjects:tobj (2), Tobj (3), Tobj (3), TOBJ ( 1), Tobj (5), nil];3 nsset *set2=[nsset setwithobjects:tobj (2), Tobj (4), Tobj (6), nil];4 [MSet minusset:set2];5 NSLog (@ " intersect:%@ ", MSet); Results: 13456
8) AllObjects is used to convert collections to arrays such as:
1 #define TOBJ (n) [NSNumber numberwithint:n]2 nsset * mSet =[[nsset alloc]initwithobjects:tobj (2), Tobj (3), Tobj (3), TOBJ ( 1), Tobj (5), nil];3 nsarray *array= [MSet allobjects];
9) Anyobject take any element in set (if there is only one element in the set, take a value)
1 #define TOBJ (n) [NSNumber numberwithint:n]2 nsset * mSet =[[nsset alloc]initwithobjects:tobj (2), Tobj (3), Tobj (3), TOBJ ( 1), Tobj (5), nil];3 ID value=[mset anyobject];
Introduction of variable index set of Nsindexset and Nsmutableindexset
1) An indexed collection that represents a collection of unique integers, with mutable and immutable points.
2) initwithindexesinrange create index objects with the corresponding index of the specified range for example:
1 Nsindexset *indexset1=[[nsindexset alloc]initwithindexesinrange:2 nsmakerange (2, 3)];//results 2,3,4
3) objectsatindexes the corresponding element in the array based on the index in the index collection (returns an array) for example:
1 Nsindexset *indexset1=[[nsindexset Alloc]initwithindexesinrange:nsmakerange (2, 3)]; 2 Nsarray *[email protected ][@ "One", @ "one", @ "three", @ "four", @ "five", @ "Sex"];3 Nsarray *array2=[array objectsatindexes:indexset1];4 NSLog (@ " array2:%@ ", array2); Results: Array2:three Four Five
4) Create a mutable collection index (there is an index on initialization) (you can store discontinuous index values) for example:
1 nsmutableindexset *indexset2=[nsmutableindexset indexsetwithindex:2];2 [IndexSet2 addindex:4];3 [IndexSet2 addIndex : 1];4 [IndexSet2 addindex:2];5 NSLog (@ "Count:%ld", indexset2.count);//Get number 6 Nsarray *array3=[array objectsatindexes: indexset2];//results: three Five
5) NSNull: class represents null, only one class method [NSNull null] Gets an empty object, and nil in the array indicates the end of the element (cannot represent empty element with nil)
You can use [NSNull null] to represent empty elements, for example:
1 nsarray *array5=[nsarray arraywithobjects:@ "Red", [NSNull null],@ "Yellow", @ "Blue", nil];
Iii. Category Introduction
1) Category meaning: Categories, categories, categories
1, you can extend the function of the class without changing the class name (adding methods to the Class)
2, you can split the function of the class into multiple files compiled
3. You cannot add member variables in a category, you can access member variables in the original class
4, the category can be added to the original class with the same name method, called when the first call
5. When adding a file, select Objective-c file and pay attention to selecting the class name to expand
2) Category declarations are similar to class declarations, @interface class names (category names) to expand functionality
1. Categories cannot instantiate objects
2. You cannot add member variables in a category.
3. Methods in the category can access member variables in the original class
4. Categories can call methods in the original class
5. Methods in the category can inherit the quilt class
6, the category can be added to the same method as the original class, call the method in the category of the first call, it is generally not recommended (can not call the method in the original class)
3) string, NSNumber is a cluster class, the bottom layer is composed of many classes, cannot have subclasses, because subclasses cannot call methods in the parent class
4) Category file name format: Parent class file name + subclass filename such as: nsmutalestring+resvrse.h
5) method representation in Category. m file: 1 @implementation nsmutablestring (Reverse)
Iv. introduction of Extension
1) Extension: Equivalent to an unnamed category, you can extend the functionality of the class (Add method), or you can increase the member variable.
2) Extension: only. h files
3) Extension Representation @interface class name () For example: 1 @interface person () {}
4) member variables can also be declared in. m files, and they will not be exposed to the consumer in the interface H file. For example:
1 @interface person () 2 {3 int _num;4}5//Declare method as private 6-(void) print2;7 @end
V. Introduction of SEL
1) SEL is a type that encapsulates the method name as a variable of the SEL, and through the SEL finds the address of the method, calling the method.
2) SEL Encapsulation Method instance code:
1 //encapsulates the play method name into SEL-type data 2 sel [email protected] (play), 3 //Determines whether the class P1 belongs to implements the method in Sel 4 if ([P1 respondstoselector: SEL]) {5 //P1 find the address of the method in the SEL, and then call the corresponding method 6 [P1 performselector:sel];7 }
3) Performselector method for performing SEL encapsulation for example: 1 [p1 performselector: @selector (jump)];
4) SEL encapsulates a method instance code with parameters:
1 //wrap a method with one parameter as a variable of SEL, execute, parameter is ID type 2 [p1 performselector: @selector (print:) withobject:@ "Hello"];
5) nsselectorfromstring is used to encapsulate the method name in the form of a string into the SEL data instance code:
1 SEL sel2=nsselectorfromstring (@ "study"); 2 [P1 PERFORMSELECTOR:SEL2];
6) _cmd represents the currently executing method for example: 1NSLog (@ "*****metheod:%@", Nsstringfromselector (_cmd));
7) in the C language __func means to obtain the current execution method such as: 1 NSLog (@ "func=%s", __func__);
__DATE__ represents the acquisition of the current system time 1 NSLog (@ "date=%s", __date__);
8) SEL implements array sort instance code:
1 void Testsel () 2 {3 person *p1;4 person *p2;5 person *p3;6 Nsmutablearray *array1=[[nsmutablearray ALLOC]INITWITHOBJECTS:P1,P2,P3, nil];7 [array1 sortusingselector: @selector (compareperson:)];8 9}
Vi. constructing a two-dimensional array in OC
Instance code:
Create an empty outer array 2 Nsmutablearray *bigarray=[nsmutablearray array]; 3//Create an array that holds 4 Data Objects 4 Nsmutablearray *array1=[[nsmutablearray Alloc]init]; 5 for (int i=0;i<4;i++) {6 [array1 addobject:[nsnumber numberwithint:i]; 7} 8//Create An array that holds 3 strings 9 Nsmutablearray *array2=[[nsmutablearray alloc]init];10 for (int i=0;i<3;i++) {11 [Array2 addobject:[nsstring stringwithformat:@ "str%d", i+1]];12}13//Put array1 and array2 two array objects into an outer array (equivalent to creating a A two-dimensional array) [Bigarray addobject:array1];15 [Bigarray addobject:array2];16 17//traversal, showing all elements 18 for (int i=0;i<bigarray.count;i++) {J=0;j<[bigarray[i] count];j++ {20// Remove the element in column I of row J of the array (each row is an array object) if ([Bigarray[i][j] Iskindofclass:[nsnumber class]) {NS Log (@ "number:%@", Bigarray[i][j]),}24 else if ([[[Bigarray ObjectatindeX:I] Objectatindex:j] iskindofclass:[nsstring class]) {NSLog (@ "string:%@", [[Bigarray Objectatindex: I] objectatindex:j]); 26}27}28}
Vii. Introduction to Class (classes)
1) The nature of the class is also an object, an object of class type, which gets the class object (which can be obtained through an instance method or a class method).
There is only one class object per class.
2) The Load method loads all classes and classifications when the program starts, calls the Load method, loads the parent class, loads the subclasses, and then classifies for example:
1 + (void) load2 {3 NSLog (@ "Person---load"); 4 }
3) Initialize method when using the class for the first time, call the Initialize method, call the parent class first, and then call the subclass for example:
1 + (void) initialize2 {3 NSLog (@ "Person---initialize"); 4 }
iOS Development--Grammar chapter OC & Advanced Grammar Explaining