クラスクラスタを(ARCで)実装したいのですが、clangが邪魔です。別のクラスの具体的なインスタンスを返す 1 つの init メソッドを次に示します。これがクラス クラスターのポイントです。
@implementation PlaceholderServer
- (Server *) init {
MockServer *concreteServer = [[MockServer alloc] init];
return concreteServer;
}
@end
そしてclangはreturn
声明に不満を述べています:
warning: incompatible pointer types returning 'MockServer *__strong' from a function with result type 'PlaceholderServer *' [-Wincompatible-pointer-types]
私はその警告の理由を理解しています:実装クラス (またはおそらくサブクラス) のインスタンスを返すことになっているメソッドinit
のファミリーに属するものとして clang によって認識されます。init
これは通常、インスタンス化する実際の具象クラスが条件によって異なる可能性があるクラス クラスターには当てはまりません。
__attribute__((objc_method_family(FAMILLY)))
clangはalloc
、メソッド ファミリーの自動認識をオーバーライドするアノテーションをcopy
提供init
しmutableCopy
ますnew
。である可能性もnone
あり、ドキュメントには次のnone
ように記載されています。
残念ながら、私の場合はうまく機能させることができません。に次の宣言を追加すると@interface
:
- (SGIServer *) init __attribute__((objc_method_family(none)));
その後、警告は消えません。属性を実装に追加すると:
- (Server *) init __attribute__((objc_method_family(none)))
{
MockServer *concreteServer = [[MockServer alloc] init];
return concreteServer;
}
その後、警告は消えず、エラーも発生します。
error: method was declared as an 'init' method, but its implementation doesn't match because its result type is unrelated to its receiver type
- (SGIServer *) init __attribute__((objc_method_family(none)))
^
両方を実行すると、最初の警告は消えませんが、追加の警告が表示されます。
warning: attributes on method implementation and its declaration must match [-Wmismatched-method-attributes]
だから私は初歩的な何かが欠けていると思いますが、何ですか?
これは Xcode 4.3.2 または Xcode 4.4DP2 です。