我不認爲這真的與ARC有關。與ARC不同的是編譯器會給你一個警告/錯誤。
一般來說,在類方法中(那些以+開頭而不是 - 並且被調用的方式不同)self並不指向特定的對象。它是指班級。使用 [self alloc] 您可以創建該類的一個對象,而不管它是否爲子類。 如果這是一個實例方法[[self class] alloc]將非常相當。但是,出於很好的理由,你正處於班級方法中。
爲什麼不在這種情況下返回結果?
+ (id)itemWithTitle:(NSString *)title target:(id)target action:(SEL)action
{
return [[self alloc] initWithTitle:title target:target action:action];
}
如果你的類名是foo,那麼你可以去:
+ (id)itemWithTitle:(NSString *)title target:(id)target action:(SEL)action
{
Foo *newMe = [[self alloc] initWithTitle:title target:target action:action];
// Here you have access to all properties and methods of Foo unless newMe is nil.
return newMe;
}
或無需訪問foo的方法和屬性更普遍的:
+ (id)itemWithTitle:(NSString *)title target:(id)target action:(SEL)action
{
id newMe = [[self alloc] initWithTitle:title target:target action:action];
return newMe;
}
謝謝!它的工作現在。我已經下載了這個地方瞭解更多關於obj-c的信息。奇怪它的工作沒有ARC,當我嘗試將它遷移到ARC時,它指出這個錯誤。一旦計時器不見了,我會將其標記爲已回答! :) – Gerdinand 2013-02-25 16:55:21
是的,它沒有ARC工作,但它是不正確的。代碼中會發生什麼:1. self指類本身的類對象。 Class實際上是Objective-C中的一個對象。 2.然後你從alloc init返回一些類型id。你將它分配給你的自我指針。從現在開始,自我不再指向班級,而是指向實例,因爲自我也會在實例方法中實現。所以根據剩餘的代碼它可以工作,但至少它是一個壞模式。 – 2013-02-25 17:00:32