Protocol (@protocol): interface equivalent to Java
Protocol methods are not necessarily implemented, you can use @required and @optional to set, the default is @required (ob is weak syntax, even if you do not implement the required method compile run will not error)
Reflection of Class
Common methods
Class variable name = [Class or object class];
Class variable name = [Class or object superclass];
Class Variable name = nsclassfromstring (string of method name);
NSString * Variable name = Nsstringfromclass (class type parameter);
Instantiating an object as a string of a class name
Class = nsclassfromstring (@ "Student"); ID obj = [[class alloc]init]; NSLog (@ " created object:%@", obj)
The introspection method provided in NSObject
-(BOOL) Iskindofclass: (Class) AClass determines whether the object belongs to the specified type or its subclasses
-(BOOL) Ismemberofclass: (Class) AClass determines whether the object is of a specified type
-(BOOL) Issubclassofclass: (Class) AClass determines whether the object is a subclass of the specified type
-(BOOL) Conformstoprotocol: (Protocol *) Aprotocol determines whether a class/object implements a protocol (parameters are passed in using @protocol (MyProtocol))
Category: How to add a new method to an existing class
Category declarations for categories
@interface ClassName (CategoryName)
-(void) addmethod;
@end
Category implementations
#import "Classname+categoryname.h"
@implementation ClassName (CategoryName)
-(void) Addmethod {
Add code to implement the method;
}
@end
Benefits of the Category
Want to extend the classes provided by the framework (no source code)
If you do not want to generate a new subclass, such as an extension to Nsarray
Easy to do project management, can be a source (non-extended Class) in multiple places to share, multi-person collaborative development
Seamless expansion of the system framework
< objective-c > Protocols, Reflections and categories