从XCode 4.2到4.3,新的ViewController类.m文件定义不同。为什么? [英] From XCode 4.2 to 4.3, new ViewController class .m files are defined differently. Why?

查看:156
本文介绍了从XCode 4.2到4.3,新的ViewController类.m文件定义不同。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

像大多数,我最近下载最新版本的XCode(4.3.1)。我注意到,由于我正在创建新的 UIViewController 对象,相关的 .m 文件包含额外的类定义代码

Like most, I recently downloaded the latest version of XCode (4.3.1). I've noticed that as I'm creating new UIViewController objects, the associated .m files contain additional class definition code that I haven't ever seen before.

具体来说,如果我创建一个新的 UIViewController 命名为'TestViewController'我得到以下 .m 文件输出。

Specifically, if I create a new UIViewController named 'TestViewController', I get the following .m file output.

\\... removed comments...
#import "TestViewController.h"

@interface TestViewController ()

@end

@implementation TestViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

\etc ...

XCode 4.3之后新添加的代码是 #import 语句下的部分:

The newly added code since XCode 4.3 is the portion under the #import statement:

@interface TestViewController ()

@end

这段代码的目的是什么?可以/应该什么都在括号内吗?是否任何代码都在 @interface @end 语句内?

What is the purpose this code? Can/should anything go within the parenthesis? Should any code go within the @interface and @end statements?

总之,将这个代码添加到模板中有什么意义?作为一个有趣的方面,当我尝试从模板创建一个 NSObject 时,上面提到的代码片段没有被添加。它可能出现与其他类型的类模板,但在我只遇到它与 UIViewController UITableViewController 对象。

In short, what was the point of adding this code to the template? As an interesting side note, when I tried creating an NSObject from a template, the above mentioned snippet of code wasn't added. It might appear with other types class templates but at the moment I've only encountered it with UIViewController and UITableViewController objects.

推荐答案

这是一个 Objective-C类扩展。它用于定义私有变量,属性和方法。

That is an Objective-C class extension. It's used to define "private" variables, properties, and methods.

这个想法是.h文件应该只包含可公开访问的属性和方法。很多时候,当编写视图控制器时,有一些方法需要/需要编写,但是这些方法不应该是公开可见的(即,这些方法只能用在.m文件中)。你在类扩展中声明这些方法,使其不在公共的.h接口。

The idea is that the .h file should only contain publicly accessible properties and methods. Very often, when writing a view controller, there are methods that you will want/need to write, but these methods shouldn't be publicly visible (i.e., these methods should only be used in your .m file). You declare these methods in the class extension to keep it out of the public .h interface.

这篇关于从XCode 4.2到4.3,新的ViewController类.m文件定义不同。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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