1>分类的作用:将一个类分为多个模块
2>分类的使用,如果要访问分类中的成员,需要将分类的头文件先引入进来。
例子:
新建一个student的类
//头文件
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface Student : NSObject
-(void)haha;
@end
NS_ASSUME_NONNULL_END
//.M文件
#import "Student.h"
@implementation Student
-(void)haha{
NSLog(@"hahahahhahah");
}
@end
新建一个Student的分类
#import "Student.h"
NS_ASSUME_NONNULL_BEGIN
@interface Student (itcast)
-(void)hehe;
@end
NS_ASSUME_NONNULL_END
//.M文件
#import "Student+itcast.h"
@implementation Student (itcast)
-(void)hehe{
NSLog(@"hehehehehehe呵呵呵");
}
@end
调用
#import <Foundation/Foundation.h>
#import "Student.h"
#import "Student+itcast.h"
#import "Student+Sb.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!");
Student *stu=[Student new];
[stu haha];
[stu hehe];
[stu tsb];
}
return 0;
}