search

Home  >  Q&A  >  body text

新手提问:Objective-C中在@implementation上加一个@interface的有什么不同?

新手提问,看别人的代码是这样写的:

@interface FirstViewController() 
-(void)timerOnActive;
-(void)getDataFromCD;
@end

@implementation FirstViewController
@synthesize userID;
@synthesize timer;

上面的代码是写在.m文件里的,我的问题是为什么不把

@interface FirstViewController() 
-(void)timerOnActive;
-(void)getDataFromCD;
@end

写在.h文件的@interface中? FirstViewController() 后面的那个括号是空的,需要填什么吗?这样写是为了补充吗?

伊谢尔伦伊谢尔伦2863 days ago651

reply all(2)I'll reply

  • 天蓬老师

    天蓬老师2017-04-21 11:18:01

    The @interface FirstViewController() here is actually a class extension, just like an anonymous category. The methods it declares must be implemented in the main @implementation code block of the corresponding class. A class may have a publicly declared API , while having additional methods declared as private for use only by the class or framework class. You can declare such a method using a category (or more than one category) in a private header file or implementation file mentioned above. This works, but the compiler cannot confirm that all declared methods are implemented.

    reply
    0
  • 高洛峰

    高洛峰2017-04-21 11:18:01

    The method written in the m file cannot be called by external classes. This method is treated as a private method and called within this class. It is hidden from the outside world.

    reply
    0
  • Cancelreply