用于.m文件的空@interface声明是什么? [英] What is the empty @interface declaration in .m files used for?

查看:111
本文介绍了用于.m文件的空@interface声明是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始了一个新的iOS 5项目并注意到每个.m文件顶部的新内容

I've started a new iOS 5 project and noticed something new at the top of each .m file

#import "HomeViewController.h"

@interface HomeViewController ()

@end

@implementation HomeViewController
@synthesize ...




  • 这是额外的@interface ...如果我有一个单独的.h文件?

  • 为什么这不会出现在iOS 5之前的项目中?

  • 我可以使用它而不是单独的.h文件吗?

  • 今后最佳做法是什么?

    • Is this extra @interface ... required if I have a separate .h file?
    • Why did this not show up in pre iOS 5 projects?
    • Can I use this instead of having a separate .h file?
    • What is the best practice for this going forward?
    • 推荐答案

      这是课程扩展。您可以使用它来声明您不希望出现在 .h 文件中。

      That's a class extension. You can use it to make declarations that you don't want to be in the .h file.

      这个许多开发人员使用过,甚至之前,他们在 .m 文件中手动添加了扩展名。所以我猜Apple在模板中包含了这个,因为它被广泛使用并且被认为是一种很好的做法。

      This was used by many developers, even before, who manually added the extension in the .m file. So I guess Apple included this in the template because it is widely used and is considered a good practice.

      实际上, .h 文件只应用于制作将从其他文件中使用的声明。您可能必须声明一些仅在 .m 文件中使用的属性,方法或常量。对于那些声明,最好在类扩展中创建它们。

      In fact, the .h file should only be used to make declarations that are going to be used from other files. You may have to declare some properties, methods or constants that will only be used inside the .m file. For those declarations, it is better to make them in the class extension.

      所以回答你的问题:



      • 如果我有一个单独的.h文件,这是额外的@interface吗?

      不,这不是必需的,但是最佳做法。

      No, it is not required but is a best practice.



      • 为什么这不会出现在iOS 5之前的项目中?

      即使这是一个常用的做法,它没有包含在模板中。

      Even if this was a commonly used practice, it was not included in the template.



      • 我可以用它而不是有一个单独的.h文件?

      没有。类扩展不会替换 .h 文件,您必须在该文件中声明类的public接口。

      No. The class extension doesn't replace the .h file where you have to declare the "public" interface of your class.



      • 今后最佳做法是什么?

      你应该在类扩展中加入所有不需要在 .m 文件。

      You should put in the class extension all the declarations that don't need to be visible outside of the .m file.

      这篇关于用于.m文件的空@interface声明是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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