Selector, jquery Selector
======================================
Selector
======================================
[Recognition selector]
SEL
1. selector (SEL) is a variable type.
2. selector is used to load member messages (member method)
People * manInBlack = [[people alloc] init];
// SEL is a selector type, such as int a, int is an integer
// Create a selector sel, and sel installs the printName method in manInBlack.
SEL sel = @ selector (printName );
// Execution Selector
[ManInBlack performSelector: sel];
// Execute a selector method with Parameters
// A colon is added.
SEL sel1 = @ selector (printName :);
[ManInBlack performSelector: sel1 withObject: @ "I am a black man, I will be invisible"];
// [ManInBlack printName: @ "I grew up" withAge: @ "25"];
// Create a selector with two parameters
SEL sel2 = @ selector (printName: withAge :);
// Execute a selector with two parameters
[ManInBlack login mselector: sel2 withObject: @ "I grew up" withObject: @ "5 years old"];
// [Note] selector method, with a maximum of two parameters
Ii. Functions of Selector
1. selector supports ios development and event-related mechanisms.
2. selector can be used for method callback (proxy)
3. selector can implement multiple functions in another way to reuse code.
// Determine which method to call during compilation
SEL sel = @ selector (test1 );
// NSSelectorFromString can be used to determine which method to call at runtime
NSString * selectorStr = [NSString stringWithFormat: @ "test % d", I];
// NSSelectorFromString can convert string into SEL Method
// NSSelectorFromString is a system method that converts nsstring to SEL type.
SEL sel2 = NSSelectorFromString (selectorStr );
Iii. isKindofClass
[Note]
1. Use variables declared by SEL to install the member Method
2. Use variables declared by Class to install types
// Load the test method into the sel variable.
SEL sel = @ selector (test );
// Load the book type into the las variable
// Class: This is a type used to hold the Class.
Class las = [book class];
NSArray * array = @ [@ "one", @ "tow", mybook, @ "jack"];
// [Question] how to retrieve the book type variable from array
For (id object in array)
{
// IsKindOfClass is used to determine whether the object is of the book type.
// IsKindOfClass is used to determine which class the object belongs.
If ([object isKindOfClass: las]) {
NSLog (@ "% @", object );
}
}
[Dynamic creation class]
[Note] NSClassFromString can be used for dynamic creation. The retrieved class must be the class in the current project.
// Load an NSString type into the variable defined by the Class
// Tell the compiler to find the class named book. h book. m in the current project and convert it into a type. Convert to a book type
// Class mybook = NSClassFromString (@ "book ");
// Book * myFirstBook = [[mybook alloc] init];
NSMutableArray * tmpArr = [[NSMutableArray alloc] init];
NSArray * classNameArray = @ [@ "surperMan", @ "food", @ "book"];
For (int I = 0; I <3; I ++ ){
NSString * C_name = classNameArray [I];
// NSClassFromString returns the class corresponding to the string
// Prerequisite: The retrieved class must be the class in the current project. If the class does not exist in the current project
// Nil is returned.
// NSClassFromString can be used for dynamic creation
Class myClass = NSClassFromString (C_name );
Id obj = [[myClass alloc] init];
[TmpArr addObject: obj];
}
Dynamic Creation has many benefits.
1. greatly simplified the amount of code
2. Make the program more powerful
3. Saving program memory