< objective-c > Protocols, Reflections and categories

Source: Internet
Author: User

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

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.