考虑以下代码:
- (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
的一部分进行复制和传递,以便于调试?
我可以想到一些可能的答案,但没有一个能让我感觉良好。我可以做NSLog(@"%@",url)
,但这会引起呕吐。我可以NSAssert(url,...);
或if (url) {}
,但这两者都不能表达我想要做的事情。
答案 0 :(得分:3)
将来电转至NSLog()
#ifdef debug.. #endif
答案 1 :(得分:1)
我想到了一个更好的答案,但感谢@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];
}
答案 2 :(得分:0)
多年后,我发现了最简单的答案。在块内,使用:
(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];
}