単一のクラス、2 つのインスタンス、2 つの別個のクラスを扱いたいのかどうかはわかりません。それぞれの例を挙げて、コードが何をしているかを説明しようとします。
単一クラス、2 つのインスタンス
フィッシュ.h
@interface Fish : NSObject
@property (nonatomic, assign) int x;
@property (nonatomic, assign) int y;
- (void) someMethod;
@end
つまり、FishA という名前の新しいオブジェクトを定義しており、そのオブジェクトには x と y の 2 つのパブリック プロパティがあります。括弧内の nonatomic と assign の 2 つの項目は、プロパティに関する追加情報をコンパイラに伝えます。基本nonatomic
的には、スレッド セーフについて心配する必要はなくassign
、プロパティがオブジェクトではなく基本型であることを意味します。
魚.m
#import "Fish.h"
@implementation Fish
@synthesize x = _x;
@synthesize y = _y;
- (id) init {
self = [super init];
if (self) {
// Initilize default values
// Only use the _x and _y in init
// no other place
_x = 0;
_y = 0;
}
return self;
}
- (void) someMethod {
// Set the values
self.x = 10;
self.y = 10;
// Access the values
NSLog(@"X: %d", self.x)
NSLog(@"Y: %d", self.y)
}
したがって、@synthesize
ステートメントは、値を設定するメソッドと値を取得するメソッドの 2 つのメソッドを作成します。上記のステートメントでは、プロパティx
のメソッドを作成するようにコンパイラに指示しています。 は、プロパティの内部ストレージ変数の名前です。プロパティと内部ストレージに別々の名前を付ける方がはるかに優れています。これにより、コードがよりクリーンになり、何が起こっているのかを理解しやすくなります。x
_x
このinit
メソッドでは、内部変数に初期値を直接設定します。init
メソッドは通常、内部変数にアクセスする唯一の場所です。
使用する
#import "Fish.h"
- (void) example {
Fish *fishA = [[Fish alloc] init];
Fish *fishB = [[Fish alloc] init];
fishA.x = 10;
fishB.x = 20;
[fishA someMethod];
[fishB someMethod];
}
クラス分け
FishA.h
@interface FishA : NSObject
@property (nonatomic, assign) int x;
@property (nonatomic, assign) int y;
@property (nonatomic, assign) int size;
- (void) someMethod;
@end
フィッシュアム
#import "FishA.h"
@implementation FishA
@synthesize x = _x;
@synthesize y = _y;
@synthesize size = _size;
- (id) init {
self = [super init];
if (self) {
// Initilize default values
// Only use the _x and _y in init
// no other place
_x = 0;
_y = 0;
_size = 10;
}
return self;
}
- (void) someMethod {
// Set the values
self.x = 10;
self.y = 10;
// Access the values
NSLog(@"X: %d", self.x)
NSLog(@"Y: %d", self.y)
}
FishB.h
@interface FishB : NSObject
@property (nonatomic, assign) int x;
@property (nonatomic, assign) int y;
@property (nonatomic, strong) NSColor *color;
- (void) someMethod;
@end
色の特性は少し異なります。color はオブジェクトであり、基本型ではないため、コンパイラに処理方法を伝える必要があります。は、処理が完了するまでこのstrong
オブジェクトを保持するようにコンパイラに指示します。もう 1 つのオプションはweak
and で、コンパイラにオブジェクトを保持しないように指示します。一般的に言えば、オブジェクトでは strong を使用します。
FishB.m
#import "FishB.h"
@implementation FishB
@synthesize x = _x;
@synthesize y = _y;
@synthesize color = _color;
- (id) init {
self = [super init];
if (self) {
// Initilize default values
// Only use the _x and _y in init
// no other place
_x = 0;
_y = 0;
_color = [NSColor blueColor];
}
return self;
}
- (void) someMethod {
// Set the values
self.x = 10;
self.y = 10;
// Access the values
NSLog(@"X: %d", self.x)
NSLog(@"Y: %d", self.y)
}
そこで、2 つの別個のクラスを作成しました。FishA には size プロパティがあり、FishB には color プロパティがあります。どちらの魚も x プロパティと y プロパティを持っています。あまりエキサイティングではありませんが、2 つのクラスの違いを生んでいます。
使用する
#import "FishA.h"
#import "FishB.h"
- (void) example {
FishA *fishA = [[FishA alloc] init];
FishB *fishB = [[FishB alloc] init];
fishA.x = 10;
fishB.x = 20;
fishA.size = 50; // Works
fishB.size = 50; // Will not work
fishA.color = [NSColor redColor]; // Will not work
fishB.color = [NSColor redColor]; // Works
}