自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iOS中@class和 #import,兩種方式的討論

移動開發(fā) iOS
我建議還是養(yǎng)成良好的import習慣,不要偷懶都把import放在h文件中,無論參與的項目大小,養(yǎng)成良好的編程習慣非常重要。

很多剛開始學習iOS開發(fā)的同學可能在看別人的代碼的時候會發(fā)現(xiàn)有部分#import操作寫在m文件中,而h文件僅僅使用@class進行聲明,不禁納悶起來,為什么不直接把#import放到h文件中呢?

這是因為h文件在修改后,所有import該h文件的所有文件必須重 新build,因此,如果把#import寫在h文件中,import該h文件的文件也就會產(chǎn)生不必要的編譯,增加編譯時間,特別是在項目文件多的情況 下。想象一下,如果只是修改一個h文件而導致上百個文件不必要的編譯,那是一件多么讓人糾結的事情。。。

對于@class只是告訴編譯器有這個class,請不要報錯或警告,因此不會給編譯造成影響。

什么時候用@class這種方式聲明比#import好呢?

stackoverflow上的高手們給了不少建議:

Randy Marsh:

When I develop, I have only three things in mind that never cause me any problems.

  1. Import super classes
  2. Import parent classes (when you have children and parents)
  3. Import classes outside your project (like in frameworks and libraries)

For all other classes (subclasses and child classes in my project self), I declare them via forward-class.

 

Justin:

Simple answer: You #import or #include when there is a physical dependency. Otherwise, you use forward declarations (@class MONClass ,struct MONStruct , @protocol MONProtocol ).

Here are some common examples of physical dependence:

  • Any C or C++ value (a pointer or reference is not a physical dependency). If you have aCGPoint as an ivar or property, the compiler will need to see the declaration ofCGPoint .
  • Your superclass.
  • A method you use.

 

最后,我建議還是養(yǎng)成良好的import習慣,不要偷懶都把import放在h文件中,無論參與的項目大小,養(yǎng)成良好的編程習慣非常重要。

責任編輯:冰凝兒
相關推薦

2011-08-08 14:13:47

iPhone XML NSXMLParse

2020-02-21 17:33:17

SparkKafka數(shù)據(jù)

2011-03-03 10:26:04

Pureftpd

2010-03-16 15:23:32

java動態(tài)載入

2021-05-27 10:57:01

TCP定時器網(wǎng)絡協(xié)議

2010-08-06 09:38:11

Flex讀取XML

2023-03-29 13:06:36

2009-06-25 13:43:00

Buffalo AJA

2010-10-21 16:24:18

sql server升

2022-03-24 23:04:37

linux靜態(tài)庫動態(tài)庫

2010-09-07 11:09:59

2011-04-02 09:48:38

深拷貝

2016-11-07 09:02:02

Malloc內(nèi)存syscall

2011-06-16 10:02:08

JAVA靜態(tài)載入

2010-07-15 14:38:55

Perl eval函數(shù)

2009-09-08 15:22:20

Spring依賴注入

2010-08-03 13:27:04

FlexBuilder

2024-02-04 09:24:45

MyBatisSQL語句Spring

2010-10-20 15:48:56

SQL Server許

2021-12-08 10:47:35

RabbitMQ 實現(xiàn)延遲
點贊
收藏

51CTO技術棧公眾號