.m中@interface和@implementation花括号之间的区别

时间:2016-05-02 01:27:07

标签: ios objective-c

我是来自Swift的Objective-C的初学者。似乎有两个不同的@interface实例,我可以在其中声明我的ivars。我的头文件中有一个,例如:

// Header file
@interface ViewController : UIViewController
{
   // declare instance variables
}

@end

我可以在我的实现文件中添加另一个,例如:

// Implementation file
@interface ViewController ()
// declare instance variables
@end

@implementation ViewController

@end

编辑:我正在学习#34;旧方式"做事,教会我在.h文件中声明私人ivars。我的困惑源于看到以.h(旧方式)和.m(新的,首选方式)宣布的伊娃。这是我所学到的(到目前为止......):

// .h
    @interface SomeClass : UIViewController

    @property (nonatomic, assign) BOOL someBool;
       // declare other properties, which will all be public
    @end

// .m
    @interface SomeClass () <UITextFieldDelegate>
       // what do I declare here?...
    @end

    @implementation SomeClass {
      // ...and what do I declare here?
    }

    // method implementations

    @end

但是,我仍然对我的。@interface@implementation大括号之间的区别感到困惑。 @matt对从不使用大括号,但@ rmaddy的回答here表明@implementation SomeClass {}没问题。那么,它是什么?

1 个答案:

答案 0 :(得分:3)

您希望它可以公开访问吗?然后写入.h文件。

如果您不希望其他类看到它,即您希望将其设为私有且仅对该类本身可见,则写入.m文件。

有关详细信息,您可以看到以下问题: Where to put iVars in "modern" Objective-C?