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

时间:2021-05-17 03:10:16

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.

方法在.h文件和.m文件中合成.h文件中声明的变量的属性。

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?

但是在一些代码中,这个@interface……@end方法也保存在.m文件中。这是什么意思?它们之间有什么区别?

Also give some words about getters and setters for the interface file that is defined in .m file...

还要对.m文件中定义的接口文件的getter和setter给出一些说明。

Thanks in Advance

谢谢提前

3 个解决方案

#1


58  

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

通常会添加一个附加的@interface,该接口定义一个包含私有方法的类别:

Person.h:

Person.h:

@interface Person
{
    NSString *_name;
}

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

Person.m:

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.

“private category”(没有名称的类别的正确名称不是“private category”,而是“class extension”).m防止编译器警告这些方法被定义。但是,因为.m文件中的@interface是一个类别,所以不能在其中定义ivars。

Update 6th Aug '12: Objective-C has evolved since this answer was written:

更新8月6日12日:Objective-C自从这个答案被写出来后就进化了:

  • ivars can be declared in a class extension (and always could be - the answer was incorrect)
  • ivars可以在类扩展中声明(并且始终可以是——答案是错误的)
  • @synthesize is not required
  • @ synthesize不是必需的
  • ivars can now be declared in braces at the top of @implementation:
  • ivars现在可以在@implementation顶部的大括号中声明:

that is,

也就是说,

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

#2


6  

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.

其概念是,如果您将.h限制为类的公共接口,然后在这个类扩展中放入私有实现细节,那么您就可以使您的项目更简洁。

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

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

@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

@Interface允许您声明私有ivars、属性和方法。因此,在这里声明的任何内容都不能从这个类之外访问。通常,您希望默认地将所有ivars、属性和方法声明为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

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

@interface Jain()
    {
        NSString *_name;
    }

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

#3


0  

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

甚至可以在.m文件中创建其他类,例如从.h文件中声明的类继承的其他小类,但是有一些细微的不同行为。您可以在工厂模式中使用它

#1


58  

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

通常会添加一个附加的@interface,该接口定义一个包含私有方法的类别:

Person.h:

Person.h:

@interface Person
{
    NSString *_name;
}

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

Person.m:

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.

“private category”(没有名称的类别的正确名称不是“private category”,而是“class extension”).m防止编译器警告这些方法被定义。但是,因为.m文件中的@interface是一个类别,所以不能在其中定义ivars。

Update 6th Aug '12: Objective-C has evolved since this answer was written:

更新8月6日12日:Objective-C自从这个答案被写出来后就进化了:

  • ivars can be declared in a class extension (and always could be - the answer was incorrect)
  • ivars可以在类扩展中声明(并且始终可以是——答案是错误的)
  • @synthesize is not required
  • @ synthesize不是必需的
  • ivars can now be declared in braces at the top of @implementation:
  • ivars现在可以在@implementation顶部的大括号中声明:

that is,

也就是说,

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

#2


6  

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.

其概念是,如果您将.h限制为类的公共接口,然后在这个类扩展中放入私有实现细节,那么您就可以使您的项目更简洁。

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

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

@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

@Interface允许您声明私有ivars、属性和方法。因此,在这里声明的任何内容都不能从这个类之外访问。通常,您希望默认地将所有ivars、属性和方法声明为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

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

@interface Jain()
    {
        NSString *_name;
    }

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

#3


0  

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

甚至可以在.m文件中创建其他类,例如从.h文件中声明的类继承的其他小类,但是有一些细微的不同行为。您可以在工厂模式中使用它