NSObject
、という名前のプロパティがないfooter
ため、コンパイラが文句を言っています。IDをにキャストして戻すことNSObject
は役に立ちません。オブジェクトが常に作成したカスタムオブジェクトになることがわかっている場合は、それにキャストバックしてフッターを呼び出すと、コンパイラは文句を言いません。実際に確認するのが一番です。以下の例を参照してください(例として、footer
プロパティを持つクラスに名前を付けたViewWithFooter
ので、適切に名前を変更します)。
- (void)handleUnpresent:(NSNotification*)note
{
ViewWithFooter view = (ViewWithFooter*)[note object];
NSParameterAssert([view isKindOfClass:[ViewWithFooter class]]);
UIView* footer = [view footer];
// Do something with the footer...
NSLog(@"Footer: %@", footer);
}
すべてがプロパティを提示する無関係なクラスが多数ある場合(つまり、同じクラス階層にない場合)footer
、必要なfooter
プロパティを使用してプロトコルを作成し、上記のコード例のプロトコルにオブジェクトをキャストするのが最適です。それをアサートすると、-footer
セレクターに応答します。
プロトコルを使用した例を次に示します。
@protocol ViewWithFooter <NSObject>
- (UIView*)footer; // this could also be a readonly property, or whatever
@end
- (void)handleUnpresent:(NSNotification*)note
{
id<ViewWithFooter> view = (id<ViewWithFooter>)[note object];
NSParameterAssert([view respondsToSelector:@selector(footer)]);
UIView* footer = [view footer];
// Do something with the footer...
NSLog(@"Footer: %@", footer);
}