如何强制未使用的变量成为块的一部分?

发布于 2024-11-07 17:51:33 字数 744 浏览 0 评论 0原文

考虑以下代码:

- (void)downloadObjectUsingURL: (NSURL *)url;
{
    id file = [self createFileForURL: url];
    Finalization objectFinalization = ^() {
        // we don't access url here, but it would be handy to see it in the debugger
        [file close];
    }
    id request = [self buildRequestFromURL: url];
    [object download: request
              toFile: file
        finalization: objectFinalization];
}

强制将 url 复制为 objectFinalization 上下文的一部分,作为 objectFinalization 的一部分复制和传递的最简单方法是什么code> 为了更容易调试?

我能想到一些可能的答案,但没有一个让我觉得好。我可以做 NSLog(@"%@",url),但这会导致溢出。我可以执行 NSAssert(url,...);if (url) {},但这两者都没有真正表达我想要的内容做。

Considering the following code:

- (void)downloadObjectUsingURL: (NSURL *)url;
{
    id file = [self createFileForURL: url];
    Finalization objectFinalization = ^() {
        // we don't access url here, but it would be handy to see it in the debugger
        [file close];
    }
    id request = [self buildRequestFromURL: url];
    [object download: request
              toFile: file
        finalization: objectFinalization];
}

What is the easiest way to force url to be copied as part of objectFinalization's context, copied and passed as part of objectFinalization for the purposes of easier debugging?

I can think of a few possible answers, but none of them strike me as good. I can do NSLog(@"%@",url), but that causes spew. I can do a NSAssert(url,...); or if (url) {}, but both of these don't really express what I'm trying to do.

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

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

发布评论

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

评论(3

稀香 2024-11-14 17:51:33

将对 NSLog() 的调用包装在 #ifdef debug.. #endif

Wrap the call to NSLog() in #ifdef debug.. #endif

我恋#小黄人 2024-11-14 17:51:33

我想到了一个更好的答案,但感谢@NSResponder 让我这样思考:

#if DEBUG
#define FORCE(a) NSParameterAssert(YES || a)
#else
#define FORCE(a)
#endif

这使得代码看起来像这样:

- (void)downloadObjectUsingURL: (NSURL *)url;
{
    id file = [self createFileForURL: url];
    Finalization objectFinalization = ^() {
        FORCE(urL);
        [file close];
    }
    id request = [self buildRequestFromURL: url];
    [object download: request
              toFile: file
        finalization: objectFinalization];
}

I thought of a nicer answer, but thanks to @NSResponder for making me think this way:

#if DEBUG
#define FORCE(a) NSParameterAssert(YES || a)
#else
#define FORCE(a)
#endif

This makes the code look like this:

- (void)downloadObjectUsingURL: (NSURL *)url;
{
    id file = [self createFileForURL: url];
    Finalization objectFinalization = ^() {
        FORCE(urL);
        [file close];
    }
    id request = [self buildRequestFromURL: url];
    [object download: request
              toFile: file
        finalization: objectFinalization];
}
送你一个梦 2024-11-14 17:51:33

多年后我发现了最简单的答案。在块内,使用:

(void)variable;

示例:

- (void)downloadObjectUsingURL: (NSURL *)url;
{
    id file = [self createFileForURL: url];
    Finalization objectFinalization = ^() {
        (void)url;
        [file close];
    }
    id request = [self buildRequestFromURL: url];
    [object download: request
              toFile: file
        finalization: objectFinalization];
}

如果您只想在调试中:

- (void)downloadObjectUsingURL: (NSURL *)url;
{
    id file = [self createFileForURL: url];
    Finalization objectFinalization = ^() {
#if DEBUG
        (void)url;
#endif
        [file close];
    }
    id request = [self buildRequestFromURL: url];
    [object download: request
              toFile: file
        finalization: objectFinalization];
}

Years later I discover the simplest answer. Inside the block, use:

(void)variable;

Example:

- (void)downloadObjectUsingURL: (NSURL *)url;
{
    id file = [self createFileForURL: url];
    Finalization objectFinalization = ^() {
        (void)url;
        [file close];
    }
    id request = [self buildRequestFromURL: url];
    [object download: request
              toFile: file
        finalization: objectFinalization];
}

If you only want in in debug:

- (void)downloadObjectUsingURL: (NSURL *)url;
{
    id file = [self createFileForURL: url];
    Finalization objectFinalization = ^() {
#if DEBUG
        (void)url;
#endif
        [file close];
    }
    id request = [self buildRequestFromURL: url];
    [object download: request
              toFile: file
        finalization: objectFinalization];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文