あなたの質問を正しく理解していれば、鍵は[自己クラス]のイディオムだと思います。
更新が現在のクラスのクラスメソッドを呼び出す方法を要求する限り、を使用できます[self class]
。のように:
Structure *newStructure = [[self class] fetchStructureByID:[currentDictionary
objectForKey:@"myId"]];
inContext:managedObjectContext];
編集:@rpetrichのコメントごとに返すようにこれをやり直しました-はるかにクリーンで、呼び出しているインスタンスのタイプが確実である限りid
、その必要性を回避します。-isKindOfClass:
-createConfiguredObject
最初の部分については、id
(任意のオブジェクトへのポインター)を返すだけで、呼び出されたのと同じクラスのインスタンスを返すことを文書化できます。次に、コードで、メソッド内の新しいオブジェクトをインスタンス化する場所で[selfclass]を使用する必要があります。
たとえば、-createConfiguredObject
呼び出されたのと同じクラスのインスタンスを返すメソッドがある場合、次のように実装されます。
// Returns an instance of the same class as the instance it was called on.
// This is true even if the method was declared in a base class.
-(id) createConfiguredObject {
Structure *newObject = [[[self class] alloc] init];
// When this method is called on a subclass newObject is actually
// an instance of that subclass
// Configure newObject
return newObject;
}
次に、これを次のようにコードで使用できます。
StructureSubclass *subclass = [[[StructureSubclass alloc] init] autorelease];
subclass.name = @"subclass";
// No need to cast or use isKindOfClass: here because returned object is of type id
// and documented to return instance of the same type.
StructureSubclass *configuredSubclass = [[subclass createConfiguredObject] autorelease];
configuredSubclass.name = @"configuredSubclass";
参考までに、私が参照し-isKindOfClass:
て適切なサブクラスにキャストしたのは次のとおりです。
Structure *structure;
// Do stuff
// I believe structure is now pointing to an object of type StructureSubclass
// and I want to call a method only present on StructureSubclass.
if ([structure isKindOfClass:[StrucutreSubclass class]]) {
// It is indeed of type StructureSubclass (or a subclass of same)
// so cast the pointer to StructureSubclass *
StructureSubclass *subclass = (StructureSubclass *)structure;
// the name property is only available on StructureSubclass.
subclass.name = @"myname";
} else {
NSLog(@"structure was not an instance of StructureSubclass when it was expected it would be.");
// Handle error
}