Cocoa:在头文件中导入和在主文件中导入有什么区别? [英] Cocoa: What's the difference between importing in the header and importing in the main file?

查看:197
本文介绍了Cocoa:在头文件中导入和在主文件中导入有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么,但有时我已经设法修复一些编译错误,最值得注意的是

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'


$ b < someClass.h从.h文件复制到.m文件中。这也已经与我遇到的一些其他问题(从我的角度来看,神秘地)与标题相关。

by moving #import "someClass.h" from the .h file into the .m file. This has also worked with a couple of other problems i've encountered that have been (mysteriously from my point of view) related to headers.

一些粗略的谷歌搜索已经回答永远不会导入头文件头,这是建议停止的地方。

Some cursory googling has turned up the answer "never import headers in header file" and that's where the advice stops.

我已经完成了这一点,或者我从某个地方拿起习惯,但我认为标题是要导入标题的地方。显然不是,但任何人都可以向我解释为什么,以及什么是首选导入头的方式?

Either I've completely made this up, or I've picked up the habit from somewhere, but I thought the header was where headers were meant to be imported. Clearly not, but can anyone explain to me why that is, and what's the preferred way of importing headers?

推荐答案

如果您需要将一个对象作为接口变量,您应该使用 @ class 指令;

而是只在实现文件中导入头文件。编译器会知道你的实例变量是指向对象的指针,但是在解析头时它不知道对象的细节。它需要知道的是,它是一个类。编译器然后可以在解析实现文件时查看类的方法;

Instead, import headers only in implementation files. The compiler will know that your instance variables are pointers to objects, but it doesn't know the details of the object when parsing the header. All it needs to know is that it's a class. The compiler can then see the methods of the class when parsing your implementation file; at that point, it does need the class, to verify that it responds to the messages you're sending.

这篇关于Cocoa:在头文件中导入和在主文件中导入有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆