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

通常我们使用


@interface interface_name : parent_class <delegates>

{

......

}

@end 

.h文件和.m文件中的方法,我们综合了.h文件中声明的变量的属性。


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


还请提供有关.m文件中定义的接口文件的getter和setter的一些信息...


提前致谢


心有法竹
浏览 751回答 3
3回答

茅侃侃

通常会添加一个附加@interface类来定义包含私有方法的类别:人.h:@interface Person{&nbsp; &nbsp; NSString *_name;}@property(readwrite, copy) NSString *name;-(NSString*)makeSmallTalkWith:(Person*)person;@end人.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{&nbsp; &nbsp; [self startThinkOfWhatToHaveForDinner];&nbsp; &nbsp; return @"How's your day?";}-(void)startThinkOfWhatToHaveForDinner{}@end“私人类别”(无名类别的专有名称不是“私人类别”,它是“类扩展”)。.m防止编译器警告已定义方法。但是,由于@interface.m文件中的。是一个类别,因此无法在其中定义ivars。12年8月6日更新:自编写此答案以来,Objective-C不断发展:ivars 可以在类扩展中声明(并且总是可以-答案不正确)@synthesize 不需要ivars现在可以在大括号顶部声明@implementation:那是,@implementation {&nbsp;&nbsp; &nbsp; &nbsp;id _ivarInImplmentation;}//methods@end
打开App,查看更多内容
随时随地看视频慕课网APP