@interface MyObject : NSObject
{
// プロパティに対応するインスタンス変数アクセサを設定したいプロパティ名(型)を宣言。典型的にはインスタンス変数に対応させるが、(例では同じであるが)名前は異なっても良いとのこと。
NSString* name;
}
@property (retain) NSString* name;
@end
一方、The Objective-C Programming Languageではsimple〜として
@interface MyClass : NSObjectとある。
@property float value;
@end
実装部ではそれぞれ、
@implementation MyObject
@synthesize name//もし異なっていれば ( = instance変数名);
@end
@interface MyClass : NSObjectとなる。後者はinterfaceも提示。
@property(copy, readwrite) NSString *value;
@end
@implementation MyClass
@synthesize value;
@end
setter、getterともに各自で実装しても良い。規定外名ならattributeに名前を設定する。
実行時に動的に確認できればよいならば、実装部で@propertyの代わりに
@dynamic name;を書いておけば良いと。
インスタンス変数の宣言についての説明が不足していた。
The Objective-C Programming Language(2011/10/12版)にしっかり書いています。
項目Declared Properties-小項目Runtime Differenceに、
There is one key difference: the modern runtime supports instance variable synthesis whereas the legacy runtime does not.とある。Objective-C Runtime Programming Guideでは、runtimeのバージョンは2つあり(modernとlegacy)、modernであるruntimeはObjective-C 2.0ととも導入された。それら差の特徴が以下(最後の追加的な記述)。
ようするにlegacyでは、継承する側のクラスは、親クラスのインスタンス変数の順序が変わった場合、リコンパイルが必要となるが、modernではその必要がない。またdeclared propertyの合成もサポートされている。
The most notable new feature is that instance variables in the modern runtime are “non-fragile”:
・In the legacy runtime, if you change the layout of instance variables in a class, you must recompile classes that inherit from it.
・In the modern runtime, if you change the layout of instance variables in a class, you do not have to recompile classes that inherit from it.
In addition, the modern runtime supports instance variable synthesis for declared properties (see Declared Properties in The Objective-C Programming Language ).
0 件のコメント:
コメントを投稿