目标c - 导入.m和.h文件 - 它做了什么 [英] Objective c - import .m and .h file - what does it do

查看:140
本文介绍了目标c - 导入.m和.h文件 - 它做了什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在目标c中,当您说#importMyClass.h时实际发生了什么? (编译器是否为您复制了什么?)

In objective c, what actually happened when you say #import "MyClass.h"? (Is the compiler copying something for you?)


  1. 在MyClass.m文件中,如果我#导入UsefulClass.h,这意味着 UsefulClass 在此文件下可用,我可以创建对象并将消息发送到它的实例。

  1. In MyClass.m file, if I #import "UsefulClass.h", this means that UsefulClass becomes available under this file and I can create object and send message to an instance of it.

在myClass.m文件中,我必须 #importMyClass.h,这听起来比如将我的实现文件链接到它的标题(称为基本文件?),这与第一个文件的作用完全不同。
那么 #import 根据具体情况做两种不同的事情?或者它实际上是从另一个角度归入一个类别。

In myClass.m file, I have to #import "MyClass.h", this sounds like linking my implementation file to its header (called base file?), which feels quite different from what the first one does. So does #import do two different kind of things based on circumstances? Or does it actually fall into one category from another perspective.

.m文件中定义的方法但不是.h文件中定义的方法被视为私有。从另一个类,我可以以某种方式调用私有方法吗? (比如我#import .m而不是.h?所以类会知道实现文件定义的内容。)

The method defined in .m file but not in .h file is considered private. From another class, can I invoke the private method somehow? (like if I #import .m instead of .h? so the class will know about what the implementation file defines.)

Objective之间有什么区别-c #import 和c #include

What is the difference between objective-c #import and c #include ?

@interface MyClass:NSObject 用于.h文件, @interface MyClass()用于.m文件。当你想拥有私有属性时,它只是一种语法格式(如括号)吗?或者这背后有什么逻辑?

Also @interface MyClass : NSObject is used in .h file and @interface MyClass() is used in the .m file. So is it just a syntax format(like the bracket) when you want to have private properties? or is there any logic behind this?


推荐答案


编译器是否为您复制了一些内容? [当你说 #importmyClass.h]?

否, 它不是。 #import 预处理程序指令几乎与 #include 指令相同,但不需要< a href =http://en.wikipedia.org/wiki/Include_guard =nofollow> 包含守护 - 从C借来的结构,看起来并不直观。

No, it does not. An #import preprocessor directive is nearly identical to the #include directive, with the exception that it does not need an inclusion guard - a construct borrowed from C, which does not look intuitive.


  1. 最简单的方法可视化当你 #import a .h header时会发生什么进入.m文件是想象该标题的内容全部放在替换 #import 的行上。从本质上讲,这就是预处理器所做的事情。

  2. 同样的事情发生在#1中 - 如果标头与.m文件相关,则与预处理器无关。 / li>
  3. 定义私有方法和私有实例变量的一种更常见的方法是在类扩展中声明它们(参见第5项)。这会保留所有声明的方法,这是一件好事。有一些方法可以在Objective-C中调用私有方法 - 你可以通过反射来实现它,或者你可以通过在不同的类上定义签名来伪造它,然后在你选择的类上调用它。编译器会发出警告,但它应该服从。

  4. 我在上面提到过,不需要使用 #import 的包含保护。

  5. 带有空括号的接口是类扩展。它就像一个类别,但它允许您向类中添加实例变量。

  1. The easiest way to visualize what happens when you #import a .h header into a .m file is to imagine that the content of that header is placed in its entirety on the line replacing the #import. Essentially, that is what the preprocessor does.
  2. The same thing happens as in #1 - it does not matter to the preprocessor if the header is related to the .m file or not.
  3. A more common way to define private methods and private instance variables is by declaring them in class extensions (see item #5). This keeps all your methods declared, which is a good thing. There are ways to invoke a private method in Objective-C - you could do it through reflection, or you could fake it by defining a signature on a different class, and then invoking it on the class of your choice. The compiler would warn, but it shall obey.
  4. I mentioned above that there is no need to use inclusion guards with #import.
  5. The interface with empty parentheses is a class extension. It is like a category, but it lets you add instance variables to your class.

这篇关于目标c - 导入.m和.h文件 - 它做了什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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