2012-04-01 19 views
0

我想實現一個類集羣(與ARC),但叮噹阻礙。這裏是返回另一個類的具體實例一個init()方法 - 這是類簇的點:叮噹聲類類:如何使用__attribute __((objc_method_family(none)))`?

@implementation PlaceholderServer 
- (Server *) init { 
    MockServer *concreteServer = [[MockServer alloc] init]; 
    return concreteServer; 
} 
@end 

而且鐺抱怨的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))),其中familly可以是alloc之一,copyinitmutableCopy,或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))) 
^ 

如果我都做,最初的警告沒有按「T消失,但我得到一個額外的警告

warning: attributes on method implementation and its declaration must match [-Wmismatched-method-attributes] 

,所以我想我失去了一些東西的Elemen tary,但是什麼?

這是與Xcode 4.3.2或Xcode 4.4DP2。

回答

0

不要弄髒屬性應該是必要的;你應該能夠代替return (id) concreteServer;,它切換到動態類型。