LoginSignup
19
16

More than 5 years have passed since last update.

Objective-C: クラスインターフェースの書き方

Last updated at Posted at 2012-06-30

内部で使うインスタンス変数はなるべく.m側に書くようにする。IBOutlet, IBAction も .h に書く必要は無い。

Hoge.h
@interface Hoge : NSObject

- (void)method;

@end
Hoge.m
#import "Hoge.h"


// @interface クラス名() @end の形で書く
@interface Hoge()
{
    IBOutlet UIView *view;
    IBOutlet UButton *button;

    id _object;
}

// メソッドのプロトタイプ宣言
- (IBAction)buttonAction:(id)sender;

- (void)_myMethod;

@end

#pragma mark -

@implementation

- (void)method {}

@end


同様に、公開する必要の無いプロパティも.m側に書ける。

Hoge.h
@interface Hoge : NSObject

// 公開用のプロパティ
@property (assign, nonatomic) NSInteger count;

- (void)method;

@end
Hoge.m
#import "Hoge.h"

@interface Hoge()
{
}

// クラス内で使うプロパティ
@property (retain, nonatomic) NSString *string;
@property (assign, nonatomic) BOOL flag;

@end

#pragma mark -

@implementation

@synthesize string;
@synthesize flag;


@synthesize count;

- (void)method {}

- (void)dealloc
{
    self.string = nil;

    [super dealloc];
}

@end


このように書けば.hの記述をすっきりさせることができる。

19
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
16