16th-day notes (Category/NSSet/NSIndexSet operation), nssetnsindexset

Source: Internet
Author: User

16th-day notes (Category/NSSet/NSIndexSet operation), nssetnsindexset

Knowledge points of IOS Learning (oc language)

 


1. Introduction to NSSet and NSMutableSet

 

1) NSSet and NSMutableSet. elements are unordered and cannot have repeated values.

 

2) use the instance method to create an immutable set object, for example:

// Macro definition # define TOBJ (n) [NSNumber numberWithInt: n] NSSet * set1 = [[NSSet alloc] initWithObjects: TOBJ (2), TOBJ (3 ), TOBJ (3), TOBJ (1), TOBJ (5), nil];

 

2) use the class method to create an immutable set object, for example:

 

1 NSSet *set2=[NSSet setWithObjects:TOBJ(2),TOBJ(4),TOBJ(6), nil];

3) NSSet fast traversal (unordered, so no subscript) Example:

 

1  for(id num in set1){2     NSLog(@"%@",num);3 } 

4) setSet is used to modify the set content, for example, [mSet setSet: set1];


5) intersectSet is used to obtain the intersection of two sets (returns the same element of the Two sets ). 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); // result: 2

6) unionSet is used to obtain the union of the two sets (all the elements in the two sets are returned. If they are repeated, only one of them is displayed.) 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); // result: 123456

7) minusSet is used to obtain the difference set 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); // result: 13456

8) allObjects is used to convert a set to an array. 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 NSArray *array= [mSet allObjects];

9) anyObject obtains any element in the set (if there is only one element in the set, set the 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];

II. Introduction to NSIndexSet and NSMutableIndexSet variable index Sets


1) An index set, which represents a set of unique integers. It can be changed or not changed.


2) initWithIndexesInRange: Create an index object with the index of the specified range. For example:

 

1 NSIndexSet * indexSet1 = [[NSIndexSet alloc] initWithIndexesInRange: 2 NSMakeRange (2, 3)]; // result 2, 3, 4

3) objectsAtIndexes retrieves the corresponding elements (returned arrays) of the Array Based on the index in the index set. For example:

 

1 NSIndexSet * indexSet1 = [[NSIndexSet alloc] initWithIndexesInRange: NSMakeRange (2, 3)]; 2 NSArray * array = @ [@ "one", @ "two ", @ "three", @ "four", @ "five", @ "sex"]; 3 NSArray * array2 = [array objectsAtIndexes: indexSet1]; 4 NSLog (@ "array2: % @ ", array2); // result: array2: three four five

4) create a variable SET index (with an index during 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); // obtain the number of NSArray * array3 = [array objectsAtIndexes: indexSet2]; // result: two three five

5) NSNull: the class indicates null. Only one class method [NSNull null] obtains the null object. In the array, nil indicates that the element ends (nil cannot be used to indicate the empty element.

[NSNull null] can be used to indicate null elements.) For example:

 

1 NSArray *array5=[NSArray arrayWithObjects:@"red",[NSNull null],@"yellow",@"blue", nil];

Iii. Introduction to Category


1) Category indicates Category, Category, and Category.

1. You can extend the functions of a class without changing the class name (add methods to the class)

2. You can split the functions of the class into multiple files for compilation.

3. The member variables cannot be added to a category. You can access the member variables in the original class.

4. You can add methods of the same name as the original class in the category.

5. Select the Objective-C File when adding the File and select the class name to be extended.


2) The category declaration is similar to the class declaration. @ interface refers to the class name (category name) of the function to be expanded)

1. Category objects cannot be instantiated

2. member variables cannot be added to a category.

3. the method in the category can access the member variables in the original class.

4. classes can call methods in the original class.

5. the method in the category can be inherited by the quilt class.

6. You can add the same method as the method in the original class. The method in the class is called preferentially. Generally, this operation is not recommended (the method in the original class cannot be called)


3) the string and NSNumber are cluster classes. The underlying layer is composed of many classes and cannot have subclasses, because subclasses cannot call methods in the parent class.


4) The Category file name format is: parent class file name + subclass file name, for example, NSMutaleString + Resvrse. h.


5) Method Representation in the Category. m file: 1 @ implementation NSMutableString (Reverse)


Iv. Introduction to Extension


1) extension: It is equivalent to an unnamed Category. It can expand the functions of the class (add methods) or add member variables.


2) extension: Only. H files


3) extension representation @ interface Class Name () Example: 1 @ interface Person (){}


4) The member variables can also be declared in the. m file and will not be exposed to the user in the interface H file. For example:

1 @ interface Person () 2 {3 int _ num; 4} 5 // declare the method as private 6-(void) print2; 7 @ end

 

V. Introduction of SEL


1) SEL is a type. It encapsulates the method name as a variable of sel, finds the method address through SEL, and calls the method.


2) SEL Encapsulation Method Instance code:

 

1 // encapsulate the play method name into SEL-type data 2 SEL sel = @ selector (play ); 3 // determine whether the class to which p1 belongs has implemented the method 4 in sel if ([p1 respondsToSelector: sel]) {5 // p1 searches for the address of the method in sel, then call the corresponding method 6 [p1 performSelector: sel]; 7}

3) performSelector: 1 [p1 performSelector: @ selector (jump)];


4) Example code of SEL encapsulation method with parameters:

 

1 // encapsulate a method with a parameter as a SEL variable and run it. The parameter is id type 2 [p1 performSelector: @ selector (print :) withObject: @ "hello"];

5) NSSelectorFromString is used to encapsulate string-form method names into SEL data instance code:

1  SEL sel2=NSSelectorFromString(@"study");2  [p1 performSelector:sel2];

6) _ cmd indicates the method currently executed, for example: 1 NSLog (@ "***** metheod: % @", NSStringFromSelector (_ cmd ));


7) in C, _ func indicates obtaining the current execution method, for example, 1 NSLog (@ "func = % s" ,__ func __);

_ DATE _ indicates obtaining the current system time 1 NSLog (@ "date = % s" ,__ DATE __);


8) SEL array sorting 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 }

6. Construct the two-dimensional array instance code in OC:

 

// Create an empty outer array 2 NSMutableArray * bigArray = [NSMutableArray array]; 3 // create an array containing four 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 containing 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 // store the array1 and array2 arrays into the outer array (equivalent to creating a two-dimensional array) 14 [bigArray addObject: array1]; 15 [bigArray addObject: array2]; 16 17 // traverse, show all elements 18 for (int I = 0; I <bigArray. count; I ++) {19 for (int j = 0; j <[bigArray [I] count]; j ++) {20 // retrieve the elements in column j of row I in the array (each row is an array object) 21 if ([bigArray [I] [j] isKindOfClass: [NSNumber class]) {22 NSLog (@ "number: % @", bigArray [I] [j]); 23} 24 else if ([[bigArray objectAtIndex: i] objectAtIndex: j] isKindOfClass: [NSString class]) {25 NSLog (@ "string: % @", [[bigArray objectAtIndex: I] objectAtIndex: j]); 26} 27} 28}

7. Introduction to Class

 

1) The essence of a Class is also an object. It is a Class object that can be obtained through instance methods or Class methods ),

Each class has only one class object.


2) the load method loads all classes and classes when the program starts. Call the load method to load the parent class, then the subclass, and then the category is as follows:

 

 1  +(void)load2  {3      NSLog(@"Person---load");4  }

3) initialize method when class is used for the first time, call the initialize method, first call the parent class, and then call the sub-class for example:

 

1 +(void)initialize2  {3     NSLog(@"Person---initialize");4  }

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.