在 Objective C 块中执行选择器

发布于 2024-10-10 14:50:42 字数 668 浏览 0 评论 0原文

我第一次尝试使用 Objective C 块是因为我非常喜欢在 Python 和 Haskell 等语言中使用闭包。

我遇到了一个问题,但我希望有人能够提供帮助。

以下是我遇到的问题的最简单版本。

typedef void(^BlockType)(NSString *string);

- (void)testWithtarget:(id)target action:(SEL)action
{
    BlockType block = ^(NSString *string) {
        [target performSelector:action withObject:data];
    };

    block(@"Test String"); // Succeeds

    [self performSelector:@selector(doBlock:) withObject:block afterDelay:5.0f];
}

- (void)doBlock:(BlockType)block
{
    block(@"Test String 2"); // Causes EXC_BAD_ACCESS crash
}

所以这似乎是某种内存管理问题,这并不令我感到惊讶,但我只是不具备了解解决方案的知识。可能我正在尝试的事情甚至是不可能的。

有兴趣看看其他人的想法:)

I have been trying to use objective c blocks for the first time because I have really enjoyed using closures in languages such as Python and Haskell.

I have run into a problem however that I am hoping someone might be able to help with.

Below is a simplest version of the problem I am having.

typedef void(^BlockType)(NSString *string);

- (void)testWithtarget:(id)target action:(SEL)action
{
    BlockType block = ^(NSString *string) {
        [target performSelector:action withObject:data];
    };

    block(@"Test String"); // Succeeds

    [self performSelector:@selector(doBlock:) withObject:block afterDelay:5.0f];
}

- (void)doBlock:(BlockType)block
{
    block(@"Test String 2"); // Causes EXC_BAD_ACCESS crash
}

So it appears to be some sort of memory management issue which doesn't suprise me but I just don't have the knowledge to see the solution. Possibly what I am trying may not even be possible.

Interested to see what other people think :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

将军与妓 2024-10-17 14:50:42

该块不会被保留,因为它只存在于堆栈中。如果您想在当前堆栈范围之外使用它(即因为您正在使用 afterDelay:),则需要复制它。

- (void)testWithtarget:(id)target action:(SEL)action
{
    BlockType block = ^(NSString *string) {
        [target performSelector:action withObject:data];
    };

    block(@"Test String"); // Succeeds

    [self performSelector:@selector(doBlock:) withObject:[block copy] afterDelay:5.0f];
}

- (void)doBlock:(BlockType)block
{
    block(@"Test String 2");
    [block release];
}

然而,这有点偶然,因为您是在方法调用之间复制和释放,但在这种特定情况下,您需要这样做。

The block is not retained, since it is only present on the stack. You need to copy it if you want to use it outside the scope of the current stack (i.e. because you're using afterDelay:).

- (void)testWithtarget:(id)target action:(SEL)action
{
    BlockType block = ^(NSString *string) {
        [target performSelector:action withObject:data];
    };

    block(@"Test String"); // Succeeds

    [self performSelector:@selector(doBlock:) withObject:[block copy] afterDelay:5.0f];
}

- (void)doBlock:(BlockType)block
{
    block(@"Test String 2");
    [block release];
}

It's a bit hap-hazard however since you're copying and releasing across method calls, but this is how you'd need to do it in this specific case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文