简体   繁体   English

目标c-导入.m和.h文件-它的作用

[英]Objective c - import .m and .h file - what does it do

In objective c, what actually happened when you say #import "MyClass.h"? 在目标c中,当您说#import“ MyClass.h”时,实际上发生了什么? (Is the compiler copying something for you?) (编译器是否正在为您复制内容?)

  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文件中,如果我#import "UsefulClass.h" ,则意味着UsefulClass在此文件下可用,并且我可以创建对象并将消息发送到它的实例。

  2. 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. 在myClass.m文件中,我必须#import "MyClass.h" ,这听起来像是将我的实现文件链接到其标头(称为基本文件?),这与第一个标头有很大的不同。 So does #import do two different kind of things based on circumstances? 那么, #import会根据情况做两种不同的事情吗? Or does it actually fall into one category from another perspective. 还是从另一角度来看实际上属于一类。

  3. The method defined in .m file but not in .h file is considered private. 在.m文件中定义但不在.h文件中定义的方法被认为是私有的。 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.) (例如,如果我#import .m而不是.h ?,那么该类将了解实现文件定义的内容。)

  4. What is the difference between objective-c #import and c #include ? Objective-c #import和c #include什么区别?

  5. Also @interface MyClass : NSObject is used in .h file and @interface MyClass() is used in the .m file. 此外,.h文件中使用@interface MyClass : NSObject ,.m文件中使用@interface MyClass() So is it just a syntax format(like the bracket) when you want to have private properties? 当您要拥有私有属性时,它是否只是一种语法格式(如括号)? or is there any logic behind this? 还是背后有逻辑?

Is the compiler copying something for you? 编译器是否正在为您复制内容? [when you say #import "myClass.h" ]? [当您说#import "myClass.h" ]?

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. #import预处理程序指令与#include指令几乎相同,不同之处在于它不需要包含防护 -从C借用的构造,看起来并不直观。

  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 . 想象当你发生什么事情最简单的方法#import一个.H头到.m文件是想象,该头的内容放置在其全部上线替换#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. 与#1中发生的事情相同-头是否与.m文件无关,对于预处理器而言都无关紧要。
  3. A more common way to define private methods and private instance variables is by declaring them in class extensions (see item #5). 定义私有方法和私有实例变量的一种更常见的方法是在类扩展中声明它们(请参阅项目#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. 有多种方法可以在Objective-C中调用私有方法-您可以通过反射来实现,也可以通过在其他类上定义签名然后在您选择的类上调用它来伪造它。 The compiler would warn, but it shall obey. 编译器将发出警告,但应遵守。
  4. I mentioned above that there is no need to use inclusion guards with #import . 上面我提到, #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. 它就像一个类别,但是它使您可以将实例变量添加到类中。

#include textually copies the contents of the named file into the place where the directive appears. #include文本方式将命名文件的内容复制到指令出现的位置。 The .h file for a class contains the interface declaration for the class, so you're copying the class's declaration. 类的.h文件包含该类的接口声明,因此您正在复制该类的声明。 In both cases (from the class's .m or another file), you are just copying the declaration. 在这两种情况下(从类的.m或另一个文件),您都只是复制声明。

As for #import , it is exactly like #include , except it keeps track of what files it has alread included so you don't end up with the same class declared a bunch of times. 至于#import ,它与#include完全一样,只是它跟踪已包含的文件,因此您不会以声明了很多次的同一类结束。

.h (header file) is the interface of your class and contains 'general description' of your class. .h(头文件)是您的类的界面,包含类的“一般描述”。 When you see the interface, you can say what your class actually do. 当您看到界面时,可以说出您的班级实际做什么。 Sometimes interface can contain a little information or being empty - that means your class can contain private methods and you can't access them through class' interface. 有时接口可能包含一些信息或为空-这意味着您的类可以包含私有方法,并且您无法通过类的接口访问它们。

.m (message file in objC , or .c in C, or .cpp in C++, or .mm in objC++) is the implementation of your class. .m(objC中的消息文件, C中的.c或C ++中的.cpp或objC ++中的.mm)是类的实现。

1 -> When preprocessor see the #import directive, it replaces the line #import "UsefulClass.h" with the text of the file 'UsefulClass.h', so your class MyClass is now "familiar" with class UsefulClass and can use its methods or variables. 1->当预处理程序看到#import指令时,它将#import "UsefulClass.h"行替换为文件'UsefulClass.h'的文本,因此您的MyClass类现在对UtilityUsedClass类“熟悉”,可以使用其方法或变量。

2 -> Remember, that class is always consists of interface and implementation (.h + .m). 2->请记住,该类始终由接口和实现(.h + .m)组成。 That's why you should make import of your header in .m file. 这就是为什么要在.m文件中导入标头的原因。 So, the #import does the same thing in first situation and in the second. 因此, #import在第一种情况下和第二种情况下执行相同的操作。

3 -> Yes, you can access to private methods. 3->是,您可以访问私有方法。 First way: you can inherit from class and all methods (private and public) of it (parent class) are made available in your child class. 第一种方法:您可以从类继承,并且该类(父类)的所有方法(私有和公共)都可以在子类中使用。 Second way: yes, you can import .m file. 第二种方式:是的,您可以导入.m文件。 BUT! 但! All these things are not recommended. 不建议所有这些事情。 By agreement you should never access to private methods. 根据协议,您永远不应访问私有方法。 When developer makes private methods, he must have a good reason for it. 当开发人员创建私有方法时,他必须有充分的理由。 In all, objC hasn't private methods. 总之,objC没有私有方法。 ObjC extension is the imitation of private method, so objC developer should be careful. ObjC扩展是对私有方法的模仿,因此objC开发人员应注意。

4 -> It's recommended to use #include in C, and #import (or @import in modern version of language) in objC. 4->建议在C中使用#include,在objC中使用#import(或在现代语言中为@import)。 From the book 'Learn Objective-C on the Mac for OS X and iOS' by Scott Knaster, Waqar Malik, Mark Dalrymple: 斯科特·纳纳特(Scott Knaster),瓦卡里·马利克(Waqar Malik),马克·达尔林普(Mark Dalrymple)撰写的“在Mac上针对OS X和iOS的Mac上学习Objective-C”一书中提到:

#import guarantees that a header file will be included only once, no matter how many times the #import directive is actually seen for that file. #import保证只将头文件包含一次,无论该文件实际看到#import指令多少次。 Note: In C, programmers typically use a scheme based on the #ifdef directive to avoid the situation where one file includes a second file, which then, recursively, includes the first. 注意:在C语言中,程序员通常使用基于#ifdef指令的方案,以避免一个文件包含第二个文件的情况,该文件随后递归地包含第一个文件。 In Objective-C, programmers use #import to accomplish the same thing. 在Objective-C中,程序员使用#import来完成同一件事。

5 -> @interface MyClass() is objC category with empty name called extension (it's only objC concept), look at the documentation to understand it. 5-> @interface MyClass()是objC类别,其空名称称为扩展名(这是唯一的objC概念),请查看文档以了解它。 But remember, that it's not actually private thing, it's only imitation. 但是请记住,这实际上不是私人物品,而只是模仿。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM