Cocoa: What's the difference between importing in the header and importing in the main file?

后端 未结 3 1446
南方客
南方客 2020-12-28 23:42

I\'ve no idea why, but on occasion I\'ve managed to fix some compile errors, most notably

error expected specifier-qualifier-list before \'someClass\'
         


        
3条回答
  •  一个人的身影
    2020-12-29 00:14

    You'll not only include your header into your implementation file but into other files using that class as well. If you include all dependencies in the header, all other files including that header just for using the specific class it defines will themselves include all dependencies as well.

    This is not only noisy but leads to problems when you have classes referencing themselves. Each class would have to be imported before the other one, which leads to one being imported first and then cannot finding the other class' type. This results in the cryptic error message you posted, which basically means that the compiler can't find the someClass type.

    By moving your imports into your implementation file and forward-declaring classes and types in your header (using @class, @protocol, etc) you can avoid these issues.

提交回复
热议问题