LoginSignup
9
9

More than 5 years have passed since last update.

自分なりのObjective-Cのコードの書き方

Last updated at Posted at 2012-11-16

基本的には、Non-ARC

Sample.h
#import <Foundation/Foundation.h>

@class CustomClass;

@interface Sample : NSObject

@property (nonatomic) BOOL flag;
@property (nonatomic, copy) NSString *string;
@property (nonatomic, retain) CustomClass *customClass;
@property (nonatomic, copy, readonly) String *readOnlyString;

@end
Sample.m
#import "Sample.h"
#import "CustomClass.h"

@interface Sample ()

@property (nonatomic, copy) NSString *readOnlyString;

@end

@implementation Sample

- (void)dealloc {
  [_string release]; _string = nil;
  [_items release]; _items = nil;
  [_readOnlyString release]; _readOnlyString = nil;
  [super dealloc];
}

- (id)init {
  self = [super init];
  if (!self) return nil;

  self.string = @"";

  return self;
}

+ (instancetype)instance {
  return [[[[self class] alloc] init] autorelease];
}

@end

循環参照を避ける為に、ヘッダファイルではフレームワーク以外のものはimportしない。

実装ファイルでimportするファイルが増えてきたら

import.h

import.h
#ifndef __import_h__
#define __import_h__

#import "aaa.h"
#import "bbb.h"

#import "zzz.h"

#endif

みたいなファイルを作ってこれを読み込む。

ここには書いていないこと

  • IBOutlet, IBActionは実装ファイル内に
  • 親クラスのヘッダファイルはxxx.hで読み込む
  • デリゲート的なものはassign
  • NSArrayNSDictionaryとかはModernな書き方に http://www.zero4racer.com/blog/798
9
9
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
9
9