What is the difference between #import and #include in Objective-C?

前端 未结 10 1502
小蘑菇
小蘑菇 2020-11-22 10:12

What are the differences between #import and #include in Objective-C and are there times where you should use one over the other? Is one deprecated?

I was reading th

10条回答
  •  孤独总比滥情好
    2020-11-22 10:57

    I agree with Jason.

    I got caught out doing this:

    #import   // to use gettimeofday() function
    #import       // to use time() function
    

    For GNU gcc, it kept complaining that time() function was not defined.

    So then I changed #import to #include and all went ok.

    Reason:

    You #import :
         includes only a part of by using #defines

    You #import :
        No go. Even though only part of was already included, as
        far as #import is concerned, that file is now already completely included.

    Bottom line:

    C/C++ headers traditionally includes parts of other include files.
    So for C/C++ headers, use #include.
    For objc/objc++ headers, use #import.

提交回复
热议问题