iOS Review note 12: Categories

Source: Internet
Author: User


1 Function description
1.1 Add a method to a class that already exists, and you cannot add a member variable.
1.2 Putting the implementation of a class into a different file
1.3 Creating a reference to a private method
2 format
Header file (file name: "Class name + category name. h")
#import "class name. h"
@interface class name (category name)
Declaration of the new method
@end


source file (file name: "Class name + category name. M")
#import "class name + category name. h"
@interface class name (category name)
New approach to implementation
@end


3 Example
Suppose you now have a person class, as follows:
Person.h#import <Foundation/Foundation.h> @interface Person:nsobject@end


Person.m
#import "Person.h" @implementation person@end



Now we want to add a eat and run method to the person class:


Person+action.h
#import "Person.h" @interface person (action)-(void) eat;-(void) run; @end


Person+action.m
#import "Person+action.h" @implementation person (action)-(void) eat{}-(void) run{}



Thus the person class has the eat and run methods, all of which can be called by two methods.

iOS Review note 12: Categories

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.