如何强制未使用的变量成为块的一部分?
考虑以下代码:
- (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将对
NSLog()
的调用包装在#ifdef debug.. #endif
中Wrap the call to
NSLog()
in#ifdef debug.. #endif
我想到了一个更好的答案,但感谢@NSResponder 让我这样思考:
这使得代码看起来像这样:
I thought of a nicer answer, but thanks to @NSResponder for making me think this way:
This makes the code look like this:
多年后我发现了最简单的答案。在块内,使用:
示例:
如果您只想在调试中:
Years later I discover the simplest answer. Inside the block, use:
Example:
If you only want in in debug: