0

クラスクラスタを(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提供initmutableCopyます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 です。

4

1 に答える 1

0

属性をいじる必要はありません。return (id) concreteServer;代わりに、動的型付けに切り替えることができるはずです。

于 2012-05-23T20:48:57.470 に答える