.h 和 .m 文件中 @interface 定义的区别
通常我们在 .h 文件中使用
@interface interface_name : parent_class <delegates>
{
......
}
@end
方法,在 .m 文件中我们综合 .h 文件中声明的变量的属性。
但在某些代码中,这个@interface.....@end方法也保存在.m文件中。这是什么意思?它们之间有什么区别?
还提供一些有关 .m 文件中定义的接口文件的 getter 和 setter 的信息...
Normally we use
@interface interface_name : parent_class <delegates>
{
......
}
@end
method in .h file and in .m file we synthesis the properties of variables declared in .h file.
But in some code, this @interface.....@end method is kept in the .m file also. What does it mean? What is the difference between them?
Also give some words about getters and setters for the interface file that is defined in .m file...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通常添加一个额外的
@interface
来定义包含私有方法的类别:Person.h:
Person.m:
The 'privatecategory' (无名类别的正确名称不是 'privatecategory' ,它是“类扩展”) .m 防止编译器警告方法已定义。但是,由于 .m 文件中的
@interface
是一个类别,因此您无法在其中定义 ivars。2012 年 8 月 6 日更新:自从写下这个答案以来,Objective-C 已经发展起来:
ivars
可以在类扩展中声明(并且总是可以 - 答案不正确)@synthesize 不是必需的
ivars
现在可以在@implementation
顶部的大括号中声明:也就是说,
It's common to put an additional
@interface
that defines a category containing private methods:Person.h:
Person.m:
The 'private category' (the proper name for a nameless category is not 'private category', it's 'class extension') .m prevents the compiler from warning that the methods are defined. However, because the
@interface
in the .m file is a category you can't define ivars in it.Update 6th Aug '12: Objective-C has evolved since this answer was written:
ivars
can be declared in a class extension (and always could be - the answer was incorrect)@synthesize
is not requiredivars
can now be declared in braces at the top of@implementation
:that is,
您甚至可以在 .m 文件中创建其他类,
例如,其他小类继承自 .h 文件中声明的类,但行为略有不同。
您可以在工厂模式中使用它
you can even create other classes in .m file,
for instance other small classes which inherit from the class declared in .h file but having some slight different behaviour.
You could use this in a factory pattern