从块内部调用 [self methodName]?
我刚刚遇到了块,我认为它们正是我正在寻找的,除了一件事:是否可以从块内调用方法 [self methodName]?
这就是我想做的:
-(void)someFunction{
Fader* fader = [[Fader alloc]init];
void (^tempFunction)(void) = ^ {
[self changeWindow:game];
//changeWindow function is located in superclass
};
[fader setFunction:tempFunction];
}
我已经搜索了几天,但找不到任何证据表明这是可能的。
这是否可能,或者我是否试图将块用于它们不适合的东西?
我使用块的原因是我创建了一个 Fader 类,并且我想存储一个块以供其在淡出完成时执行。
谢谢
编辑: 好吧,我在建议中添加了,但我仍然收到 EXC_BAD_ACCESS 错误...
-(void)someFunction{
Fader* fader = [[Fader alloc]init];
__block MyScreen* me = self;
void (^tempFunction)(void) = ^ {
[me changeWindow:game];
//changeWindow function is located in superclass
};
[fader setFunction:tempFunction];
[fader release];
}
也许我不被允许为 fader 提供该功能...?
I've just run into blocks and I think they are just what I'm looking for, except for one thing: is it possible to call a method [self methodName] from within a block?
This is what I'm trying to do:
-(void)someFunction{
Fader* fader = [[Fader alloc]init];
void (^tempFunction)(void) = ^ {
[self changeWindow:game];
//changeWindow function is located in superclass
};
[fader setFunction:tempFunction];
}
I've been searching for a couple of days and I can't find any evidence that this is possible.
Is this at all possible, or am I trying to use blocks for something they aren't meant for?
The reason I'm using blocks is that I've created a Fader class, and I want to store a block for it to execute when it finishes fading out.
Thank you
EDIT:
Okay, I added in the suggestion, but I'm still getting an EXC_BAD_ACCESS error...
-(void)someFunction{
Fader* fader = [[Fader alloc]init];
__block MyScreen* me = self;
void (^tempFunction)(void) = ^ {
[me changeWindow:game];
//changeWindow function is located in superclass
};
[fader setFunction:tempFunction];
[fader release];
}
Maybe I'm not allowed to give fader the function...?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
是的,你可以这样做。
但请注意,该块将保留
self
。如果您最终将此块存储在 ivar 中,则可以轻松创建一个保留周期,这意味着两者都不会被释放。要解决这个问题,您可以这样做:
__block
关键字意味着(除其他外)引用的对象将不会被保留。Yes, you can do this.
Note, however, that the block will retain
self
. If you end up storing this block in an ivar, you could easily create a retain cycle, which means neither would ever get deallocated.To get around this, you can do:
The
__block
keyword means (among other things) that the referenced object will not be retained.接受的答案已过时。在这种情况下使用 __block 可能会导致错误!
为了避免这个问题,最好的做法是捕获对
self
的弱引用,如下所示:请查看Apple 文档 - 避免强引用循环当捕捉自我时
了解更多详情。
The accepted answer is outdated. Using
__block
in that case can cause errors!To avoid this problem, it’s best practice to capture a weak reference to
self
, like this:Please, look at Apple Documentation - Avoid Strong Reference Cycles when Capturing self
for more details.
是的,为什么不呢。如果您的
tempFunction
是实例方法,则可以执行此操作。被调用的方法应该是可访问的,这是唯一的限制。Yes, why not. If your
tempFunction
is an instance method, you can do it. The called method should be accessible is the only restriction.考虑这一点(我认为这是最佳实践)
此外,您可以定义包装器宏。
Consider this (which I think is the best practice)
Moreover, You can define wrapper macros.
我想知道你是否 [fader setFunction:tempFunction];那么是同步还是异步。
块推入堆栈。所以在MRR中,如果你不保留它,它就会弹出。
I wonder whether you [fader setFunction:tempFunction]; then is synchronous or asynchronous.
blocks push onto stack.so in MRR,if you don't retain it,it will pop off.