我有一個解決方案,我不特別喜歡,但它可能會引發更好的答案。我會留下這個未解決的問題,希望能有更好的解決方案。
下面是做這件事:
// Here's a method definition…
-(void) aMethod
{
// Want to create a block in which its impossible to refer to strong "self".
// Begin a new scope to do this in.
{
// Within this scope, cover the existing "self" with a weak variant.
__weak STWeatherTableViewController const *weakSelf = self;
__weak STWeatherTableViewController const *self = weakSelf;
// Sadly it's _not_ possible to simply do:
// __weak STWeatherTableViewController const *self = self;
// … it gives a warning about initialisation of a variable form its own
// uninitialised value, which makes sense, though you might hope the
// compiler would work out what's going on.
// Make a block that captures the covered self and does something with it.
void (^exampleBlock)() = ^(){ [self lineHeight]; };
exampleBlock();
}
// Now, back in the scope of the original method, "self" is non weak
// again.
[self doSomething];
}
我想,如果你真的關心了很多關於這一點,你可以使用宏。這將至少抽象的想法,並使用很容易找到,在代碼中聲明:
#define WEAKEN_SELF(classType) \
__weak classType const *weakSelf = self; \
__weak classType const *self = weakSelf
甚至:
#define WEAKEN_SELF(classType) \
__weak classType const *weakSelfTemporary##__LINE__ = self; __weak classType const *self = weakSelfTemporary##__LINE__;
,你會使用這樣的:
-(void) testMethod
{
// You still need that scope or you cover the original "self".
{
WEAKEN_SELF(STWeatherTableViewController)
void (^exampleBlock)() = ^(){ [self someMethodOrOther]; };
exampleBlock();
}
}
雖然我不相信這是值得的。編譯器警告可能已經足夠好了,他們可能會被誤解爲錯誤?
你可以得到一個警告..會有幫助嗎? –
最新版本的Xcode + ARC將以塊爲單位檢測強參考週期並給出警告。 –
警告很有幫助,謝謝。我有一個解決方案,我會發布。這並不完美,但它可能會引發更好的解決方案。 – Benjohn