#import #include @class的区别
来源:互联网 发布:mac版spss23 编辑:程序博客网 时间:2024/05/01 10:22
一、#import和#include的区别
当我们在代码中使用两次#include的时候会报错:因为#include相当于拷贝头文件中的声明内容,所以会报重复定义的错误,但是使用两次#import的话,不会报错,所以他可以解决重复导入的问题,他会做一次判断,如果已经导入一次就不导入了
二、#import与@class的区别1.当A->B,B->A时,如果在B头文件中采用#import a.h,同时在A头文件中采用#import b.h时,会出现相互导入报错的问题,而@class关键字的使用,为了解决相互导入的问题
2.@class可以节省编译时间,在.h头文件中进行声明时用#import的话,如果100个头文件都#import同一个头件,或者这些文件是依次引用的,如A->B,B->C,C->D,当最开始的那个头文件有变化后进行编译时,后面所有引用它的类都需要重新编译,如果引用最开始的头文件的类很多的话,那么这将耗费大量的时间,而用@class则不会,可能有人会想即然.h只是用@class只是简单的一个声明告编译器有这个类不让其报错,那么.m中要用到引入的类的方法和属性时,不还是要#import头文件一次,是的这个是对的,但编译器编译的时候只编译头文件的,所以你的.m中用#import与编译时间没太大关系
总结:
说说什么时候该用@class,什么时候该用#import进行声明,
(1)一般如果有继承关系的用#import,如B是A的子类那么在B中声明A时用#import
(2) 另外就是如果有循环依赖关系,如:A->B,B->A这样相互依赖时,如果在两个文件的头文件中用#import分别声明对方,那么就会出现头文件循环利用的错误,这时在头文件中用@class声明就不会出错
(3)还有就是自定义代理的时候,如果在头文件中想声明代理的话如@interface SecondViewController:UIViewController时应用#import不然的话会出错误,注意XXXXDelegate是自定义的
(4)在头文件中增加代理方法时,要用到本身的类的话,要在头文件中采用@class方法。
//
// MessageCell.h
// WeChat
//
// Created by zhengwenming on 16/6/4.
// Copyright © 2016年 zhengwenming. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "JGGView.h"
#import "CommentCell.h"
@class MessageCell;
@class MessageModel;
@protocol MessageCellDelegate <NSObject>
- (void)passCellHeightWithMessageModel:(MessageModel *)messageModel commentModel:(CommentModel *)commentModel atCommentIndexPath:(NSIndexPath *)commentIndexPath cellHeight:(CGFloat )cellHeight commentCell:(CommentCell *)commentCell messageCell:(MessageCell *)messageCell;
@end
@interface MessageCell : UITableViewCell
@property (nonatomic, strong) JGGView *jggView;
/**
* 点击文字的block
*/
@property (nonatomic, copy)void(^TapTextBlock)(UILabel *desLabel);
/**
* 点击删除的block
*/
@property (nonatomic, copy) void(^deleteBtnClickBlock)(NSString *DynamicID,NSString *AppUserID);
/**
* 点击头像进入相应的用户动态
*/
@property (nonatomic, copy) void(^iconBtnClickBlock)(NSString *AppUserID);
@property (nonatomic, weak) id<MessageCellDelegate> delegate;
- (void)configCellWithModel:(MessageModel *)model indexPath:(NSIndexPath *)indexPath;
@end
- #import、#include、@class、#import<> 、#import""的区别
- #include,#import,@class的区别 .....
- #include,#import,@class的区别
- #import #include @class的区别
- #import ,#include,@class 的区别
- @class,#import,#include的区别
- #include,#import,@class的区别
- #import #include @class的区别
- #include #import @class的区别
- #include #import @class区别
- include、import、@class区别
- #Import #include @class区别
- #import、#include和@class的区别
- #import 与 #include的区别,以及 @class
- #import 与 #include的区别,以及 @class
- #import、#include和@class的区别
- iOS 开发 #import #include @class 的区别
- #import、#include和@class的区别
- 海底高铁(差分)
- MapRedcue例程编译和执行
- dubbo 使用 学习二(spring+dubbo+zookeeper单机服务)
- What is aliasing and what causes it?
- 完美兼容 英雄联盟 穿越火线 DNF 等游戏专用 装机员win10 64位系统
- #import #include @class的区别
- 瀑布流实现
- awt和Swing
- const限定符
- 第八天学习笔记
- 分治-最接近点问题
- 文章标题
- String literal is not properly closed by a double-quote
- PostgreSQL数据库导出导入