第六章、源文件组织

1.拆分接口和实现

类的定义中,代码很自然的被拆分为接口和实现两个部分,所有类的代码通常分别放在两个文件中

  • 存放接口部分的代码:类的@interface指令,公共的struct定义,enum常量,#defines和extern全局变量等,将其放在头文件中,头文件名称与类名相同,只是用.h做后缀
  • 存放实现部分的代码:类的@implementation指令,全局变量的定义,私有struct等,将其放在实现文件中,实现文件与类名相同,只是用.m做后缀

图片示例

New F[图片上传中...(屏幕快照 2018-10-06 09.23.12.png-684e52-1538843150512-0)] ile

Cocoa Class

命名

2.拆分Car程序

简单的拆分

//main.m 文件
#import 
#import "Engine.h"
#import "Tire.h"
//Car类
@interface Car : NSObject{
    Engine *engine;
    Tire *tires[4];
}
-(void)print;
@end
@implementation Car
//初始化实例变量的init方法
-(id)init{
    if (self = [super init]) {
        engine = [Engine new];
        tires[0] = [Tire new];
        tires[1] = [Tire new];
        tires[2] = [Tire new];
        tires[3] = [Tire new];
    }
    return self;
}
//通过NSLog()输出实例变量,记住%@只是调用每个对象的description
-(void)print{
    NSLog(@"%@",engine);
    NSLog(@"%@",tires[0]);
    NSLog(@"%@",tires[1]);
    NSLog(@"%@",tires[2]);
    NSLog(@"%@",tires[3]);
}
@end//Car
int main(int argc, const char * argv[]) {
    Car *car;
    car = [Car new];
    [car print];
    return 0;
}
//类.h文件
//Engine类
#import 

@interface Engine : NSObject

@end
//Tire类
#import 

@interface Tire : NSObject

@end
//类.m文件
//Engine类
#import "Engine.h"

@implementation Engine
- (NSString *) description{
    return (@"I am  a engine");
}
@end
//Tire类
#import "Tire.h"

@implementation Tire
- (NSString *) description{
    return (@"I am  a tire");
}
@end
//运行结果
2018-10-06 09:34:11.409383-0700 Car-拆分-简单[627:15686] I am  a engine
2018-10-06 09:34:11.409995-0700 Car-拆分-简单[627:15686] I am  a tire
2018-10-06 09:34:11.410040-0700 Car-拆分-简单[627:15686] I am  a tire
2018-10-06 09:34:11.410064-0700 Car-拆分-简单[627:15686] I am  a tire
2018-10-06 09:34:11.410175-0700 Car-拆分-简单[627:15686] I am  a tire
Program ended with exit code: 0

可能的错误及解决方法

出现clang: error: linker command failed with exit code 1 (use -v to see invocation)错误的解决方法

3.使用跨文件依赖关系

依赖关系是两个实体之间的一种关系.依赖关系可以存在于两个类之间,也可以存在于两个或多个文件之间

复杂的拆分

文件

//main.m 文件
#import 
#import "Engine.h"
#import "Tire.h"
#import "Car.h"
int main(int argc, const char * argv[]) {
    Car *car;
    car = [Car new];
    [car print];
    return 0;
}
//类.h文件
//Engine类
#import 

@interface Engine : NSObject

@end
//Tire类
#import 

@interface Tire : NSObject

@end
//Car类
#import 
#import "Engine.h"
#import "Tire.h"
@interface Car : NSObject{
    Engine *engine;
    Tire *tires[4];
}
-(void)print;
@end
//类.m文件
//Engine类
#import "Engine.h"

@implementation Engine
- (NSString *) description{
    return (@"I am  a engine");
}
@end
//Tire类
#import "Tire.h"

@implementation Tire
- (NSString *) description{
    return (@"I am  a tire");
}
@end
//Car类
#import "Car.h"
@implementation Car
//初始化实例变量的init方法
-(id)init{
    if (self = [super init]) {
        engine = [Engine new];
        tires[0] = [Tire new];
        tires[1] = [Tire new];
        tires[2] = [Tire new];
        tires[3] = [Tire new];
    }
    return self;
}
//通过NSLog()输出实例变量,记住%@只是调用每个对象的description
-(void)print{
    NSLog(@"%@",engine);
    NSLog(@"%@",tires[0]);
    NSLog(@"%@",tires[1]);
    NSLog(@"%@",tires[2]);
    NSLog(@"%@",tires[3]);
}
@end
//运行结果
2018-10-06 09:55:42.394801-0700 Car-拆分-复杂[838:23762] I am  a engine
2018-10-06 09:55:42.395053-0700 Car-拆分-复杂[838:23762] I am  a tire
2018-10-06 09:55:42.395071-0700 Car-拆分-复杂[838:23762] I am  a tire
2018-10-06 09:55:42.395079-0700 Car-拆分-复杂[838:23762] I am  a tire
2018-10-06 09:55:42.395085-0700 Car-拆分-复杂[838:23762] I am  a tire
Program ended with exit code: 0

4.@class

在学习过程中,我们认识了文件的依赖关系,在这种关系中,头文件或源文件需要使用另一个头文件的信息.文件导入过于混乱会延长编译时间,也会导致不必要的重复编译,而巧妙地使用@class指令告诉编译器“相信我,你最终肯定会了解这个名称的类”,可以减少必须导入的头文件的数量,从而可以缩短编译时间

示例:

//Car类的接口
#import 
@class Tire;
@class Engine;
@interface Car : NSObject{
    Engine *engine;
    Tire *tires[4];
}
-(void)print;
@end
//这样就可以告诉编译器处理Car类的@interface部分所需要的全部信息了

你可能感兴趣的:(第六章、源文件组织)