5

DataManager我在 99% 的项目中使用我的单例类。我没有在每个类中都导入它,而是考虑将它导入到 pch 文件中,就像我对常量所做的那样。这有什么缺点吗?它被认为是不好的做法吗?

谢谢

#import <Availability.h>

#ifndef __IPHONE_5_0
#warning "This project uses features only available in iOS SDK 5.0 and later."
#endif

#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
    #import "Constants.h"
    #import "DataManager.h"
#endif
4

2 回答 2

3

对#importing XYZ.h 的良好实践的评估将是,在项目的整个生命周期中,

NSTimeInterval saved = (time that would have been spent compiling XYZ.h plus every header it #imports in each file that included it every build);

NSTimeInterval wasted = (time spent recompiling files that do not include XYZ.h when XYZ.h changed);

if (saved > wasted) goodPractice = YES;

所以任何系统#import,很可能你的大部分库#imports,都是优秀的候选者。还要别的吗; 好吧,如果你的 95% 的代码是用那个特定的头文件重新编译的,那么肯定把它放在那里是有意义的。关于这个主题有一篇很好的文章。

于 2013-07-15T21:43:52.843 回答
1

Its not bad to include an import in the pch if the header doesn't change much (and also when the import is not used in most files which is not your case). Check the accepted answer for this question and decide-

Is there a reason for which one should not add all the imports inside .pch file?

于 2013-07-15T08:35:10.427 回答