In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you the "Objective-C more example analysis", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "Objective-C more example analysis" this article.
1. Objective-C polymorphism
1. Concept: same interface, different implementation
Methods that share the same name can be defined from different classes.
Dynamic typing enables the program to determine the type of object until it is executed
Dynamic type binding enables the program not to determine the actual method to be called on the object until it is executed
Different from the traditional programming language, 2.Objective-C can add new data types and new program modules at run time: dynamic type recognition, dynamic binding, dynamic loading.
3.id types: general pointer types, weak types, no type checking at compile time
Second, dynamic type recognition
1. Any subclass of NSObject inherits the isa instance variable of NSObject, and when a subclass of NSObject instantiates an object, the isa instance variable is always the first instance variable of the object.
two。 Class object
* the class object always exists when the program is running.
* A class object is a data structure that stores basic information about a class: class size, class name, class version, and mapping tables for messages and functions.
* the information saved by the class object is determined when the program is compiled and loaded into memory when the program starts.
* Class objects represent classes, class represent class objects, and class methods belong to class objects.
* if the recipient of the message is a class name, the class name represents the class object
* at run time, all instances of the class are generated by the class object, which modifies the value of the instance's isa to its own address, and the isa of each instance points to the instance's class object. * you can know the parent class information, methods that can respond, etc., from the class object.
* Class objects can only use class methods, not instance methods
3.SEL Typ
When Objective-C compiles, it generates a unique ID that distinguishes the method according to the name of the method (including the parameter sequence). This ID is of type SEL and finds the method at run time through the tag of the method. As long as the name of the method (including the parameter sequence) is the same, then their ID is the same. You can get the identification of the method through the @ select () indicator. SEL mydraw = @ select (draw)
NSSelectorFromString (NSString*); get the method ID based on the method name
(NSString*) NSStringFromSelector (SEL); get the method name of type SEL
4. Common methods of dynamic type recognition
-(BOOL) whether isKindOfClass:classObj is a classObj class or its subclass
-(BOOL) whether isMemberOfClass:classObj is an instance of classObj
-(BOOL) whether this method exists in the respondsTosSelector:selector class
NSClassFromString (NSString*); get the class object from the string
NSStringFromClass ([class name Class]); get the string from the class name
Class rectClass= [Rectangle class]; get the class object by the class name
Class aClass = [anObject class]; get the class object through the instance
If ([obj1 class] = = [obj2 class]) determines whether it is a similar instance.
5. Objects can be divided into id types and static types
-if polymorphism is not involved, try to use static types
Static types are better at pointing out errors at compile time than at run time
-static types can improve the readability of programs
Third, dynamic binding
1. In objective-c, whether a specified method is called within an object is decided not by the compiler but by the runtime, which is called the dynamic binding of the method.
two。 In objective-c, the object receives the message instead of calling the method, and the message expression is: [reciver message]; the runtime system first determines the type of the recipient (dynamic type identification), and then selects the dependent method execution in the method list of the class according to the message name, so the message is also called selector in the source code.
3. The function of the message function:
-first find its isa pointer through the receiver of the first parameter, and then use the second parameter selector lookup method in the Class object pointed to by isa
-if it is not found, use the new isa pointer in the current Class object to look in the Class object of the parent class at the next level
-when a method is found, the current object is found according to the self pointer in receiver, the specific implementation method of the current object (IMP) is called, and the parameters are passed to call the implementation method.
-if the Class object of NSObject is found all the time and the method you call is not found, an error that does not recognize sending messages will be reported.
4. Method structure in Objetive-C
Struct objc_method {
SEL method_name;// method name
Char * method_types; / / method address
IMP method_imp; / / method address (IMP)
}
Typedefobjc_method Method
5. What is IMP?
-IMP is the abbreviation of "implementation". It is the address of the code block of the objetive-C method (method) implementation, similar to a function pointer, through which any method can be accessed directly. Free of the cost of sending messages.
6. Get the IMP of the method
-(IMP) methodForSelector: (SEL) aSelector
SEL print_sel = NSSelectorFromString (@ "print:"); / / get SEL IMP imp= [person methodForSelector:print_sel]; / / get IMP imp (person,print_sel,@ "*"); / / call method equivalent via IMP: [person print_sel:@ "*"]
The first parameter of-imp is the object itself (self), the second parameter is the method tag, and the third parameter is the method parameter.
Dynamic loading: loading new classes at run time
To create a new class at run time, it only takes three steps:
1. Allocate storage space for class pair and use the objc_allocateClassPair function
2. Use the class_addMethod function to increase the required methods and class_addIvar to add the real example variables.
3. Register this class with the objc_registerClassPair function so that it can be used by others.
Note: to use these functions, please quote # import
The above is all the content of this article "Objective-C more example Analysis". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.