用 Objective-C语言编写“类”(class)的标准方式为:以类名做文件名,分别创建两个文件,头文件后缀用h,实现文件后缀用,m。创建好一个类之后,其代码看上去如下所示:
//EOCPerson. h
#import
@interface EOCPerson :NSObject
@property (nonatomic, copy) NSString *firstName;
@property (nonatomic, copy) NSString *lastName;
@end
∥ EOCPerson.m
#import "EOCPerson.h"
implementation EOCPerson
//Implementation of method
@end
增加EOCEmployer的新类
//EOCPerson. h
#import
//常见的方法是加入
#import "EOCEmployer.h"
@interface EOCPerson :NSObject
@property (nonatomic, copy) NSString *firstName;
@property (nonatomic, copy) NSString *lastName;
@property (nonatomic, strong) EOCEmployer *employer;
@end
更优雅的写法-使用"向前声明",声明该类
//EOCPerson. h
#import
@class EOCEmployer;
@interface EOCPerson :NSObject
@property (nonatomic, copy) NSString *firstName;
@property (nonatomic, copy) NSString *lastName;
@property (nonatomic, strong) EOCEmployer *employer;
@end
∥ EOCPerson.m
#import "EOCPerson.h"
//在此加入EOCEmployer引用
#import "EOCEmployer.h"
implementation EOCPerson
//Implementation of method
@end
@class一般情况下写在.h文件中,告诉编译器这是一个类,不会进行拷贝类。在.m中使用的时候在进行#Improt ,会提升编译效率。切记,要使用类,在.m中别忘记引用。