在实现文件中声明接口(Objective-C) [英] Declare interface inside implementation file (Objective-C)

查看:101
本文介绍了在实现文件中声明接口(Objective-C)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在最后一个版本的ox xCode(4.3)中,我看到了预定义的模板(例如我们的主/明细模板),其中在.m文件中进行了接口声明。例如,在文件MyFile.h中有:

In the last verson ox xCode (4.3) I've seen that prefdefined templates (such us Master/Detail template) in which the interface declaration is made in the .m file. For example, in the file MyFile.h there is:

@interface MyFile

@property (nonatomic, retain) NSString *someProp;

@end

在MyFile.m文件中有:

And in the MyFile.m file there is:

@implementation MyFile

@interface MyFile {
    NSString * anotherProp;
}

- (id) init...

为什么这样做?为什么没有将anotherProp声明为MyFile.h文件?

Why it's made on this way? Why the anotherProp isn't declared into the MyFile.h file?

提前致谢

推荐答案

好吧,不是这样宣布的,但是这样: -

Well its not declared this way but this way :-

@interface ClassName() {

    Declarations;

}

Methods;

@end

这些被称为类扩展。它们与类别类似但是只能在类的实现中声明而不能在任何其他类中声明。扩展的使用是重新声明属于public或readwrite的属性,如果需要也声明更新的属性。它们只是允许你在其他地方声明属性和变量比@interface所以名称为extensios。

These are called class extension.They are similar to categories but can be declared only in implementation of the class not in any other class.The use of extensions is to redeclare property that is public or readwrite , also declare newer ones , if needed.They simply allow you to declare properties and variables in places other than @interface so the name extensios.

它用于解决类别问题,因为它们使方法公开,类的数据隐藏功能得到补偿但是类扩展有效地扩展了类的主接口,声明的方法与类的公共主接口中声明的方法具有相同的要求。

It was inrtoduced to tackle the problem with categories as they make the methods public and data hiding capability of classes is compensated but a class extension effectively extends the class’s primary interface which the declared methods have the same requirements as methods declared in the class’s oft public primary interface.

这篇关于在实现文件中声明接口(Objective-C)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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