.h 和 .m 文件中 @interface 定义的区别

发布于 2024-09-27 17:38:36 字数 280 浏览 8 评论 0原文

通常我们在 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清旖 2024-10-04 17:38:36

通常添加一个额外的 @interface 来定义包含私有方法的类别:

Person.h:

@interface Person
{
    NSString *_name;
}

@property(readwrite, copy) NSString *name;
-(NSString*)makeSmallTalkWith:(Person*)person;
@end

Person.m:

@interface Person () //Not specifying a name for the category makes compiler checks that these methods are implemented.

-(void)startThinkOfWhatToHaveForDinner;
@end


@implementation Person

@synthesize name = _name;

-(NSString*)makeSmallTalkWith:(Person*)person
{
    [self startThinkOfWhatToHaveForDinner];
    return @"How's your day?";
}


-(void)startThinkOfWhatToHaveForDinner
{

}

@end

The 'privatecategory' (无名类别的正确名称不是 'privatecategory' ,它是“类扩展”) .m 防止编译器警告方法已定义。但是,由于 .m 文件中的 @interface 是一个类别,因此您无法在其中定义 ivars。

2012 年 8 月 6 日更新:自从写下这个答案以来,Objective-C 已经发展起来:

  • ivars 可以在类扩展中声明(并且总是可以 - 答案不正确)
  • @synthesize 不是必需的
  • ivars 现在可以在 @implementation 顶部的大括号中声明:

也就是说,

@implementation { 
     id _ivarInImplmentation;
}
//methods
@end

It's common to put an additional @interface that defines a category containing private methods:

Person.h:

@interface Person
{
    NSString *_name;
}

@property(readwrite, copy) NSString *name;
-(NSString*)makeSmallTalkWith:(Person*)person;
@end

Person.m:

@interface Person () //Not specifying a name for the category makes compiler checks that these methods are implemented.

-(void)startThinkOfWhatToHaveForDinner;
@end


@implementation Person

@synthesize name = _name;

-(NSString*)makeSmallTalkWith:(Person*)person
{
    [self startThinkOfWhatToHaveForDinner];
    return @"How's your day?";
}


-(void)startThinkOfWhatToHaveForDinner
{

}

@end

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 required
  • ivars can now be declared in braces at the top of @implementation:

that is,

@implementation { 
     id _ivarInImplmentation;
}
//methods
@end
初心未许 2024-10-04 17:38:36

这个概念是,如果你
将 .h 限制为类的公共接口,然后将
此类扩展中的私有实现细节。

当您在 ABC.h 文件中声明变量方法或属性时,它
意味着这些变量属性和方法可以在外部访问

@interface Jain:NSObject
{
    NSString *_name;
}

@property(readwrite, 复制) NSString *name;
-(NSString*)makeSmallTalkWith:(Person*)jain;
@结尾

@Interface 允许您声明私有 ivars、属性和
方法。所以你在这里声明的任何东西都不能从外部访问
这堂课。一般来说,您想要声明所有 ivars、属性和
方法默认为私有

简单地说当你在ABC.m中声明变量方法或属性时
文件,意味着这些变量的属性和方法不能被
类外访问

<前><代码>@interface Jain()
{
NSString *_name;
}

@property(readwrite, 复制) NSString *name;
-(NSString*)makeSmallTalkWith:(Person*)jain;
@结尾

The concept is that you can make your project much cleaner if you
limit the .h to the public interfaces of your class, and then put
private implementation details in this class extension.

when you declare variable methods or properties in ABC.h file , It
means these variables properties and methods can be access outside the
class

@interface Jain:NSObject
{
    NSString *_name;
}

@property(readwrite, copy) NSString *name;
-(NSString*)makeSmallTalkWith:(Person*)jain;
@end

@Interface allows you to declare private ivars, properties and
methods. So anything you declare here cannot be accessed from outside
this class. In general, you want to declare all ivars, properties and
methods by default as private

Simply say when you declare variable methods or properties in ABC.m
file , It means these variables properties and methods can not be
access outside the class

@interface Jain()
    {
        NSString *_name;
    }

    @property(readwrite, copy) NSString *name;
    -(NSString*)makeSmallTalkWith:(Person*)jain;
    @end
放低过去 2024-10-04 17:38:36

您甚至可以在 .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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文