NSURLConnection 的 Xcode 4 警告“未使用表达式结果”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7914990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode 4 warning "Expression result unused” for NSURLConnection
提问by Johann
I'm just trying to do my usual data transfert. I define my NSMutableURLRequest then call
我只是想进行我通常的数据传输。我定义了我的 NSMutableURLRequest 然后调用
[[NSURLConnection alloc] initWithRequest:request delegate:self];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
This used to be ok with Xcode 3 but Xcode 4 warns me about "Expression result unused" on that line. The request does work but I would like to find a way to get rid of the warning.
这曾经适用于 Xcode 3,但 Xcode 4在该行警告我“未使用的表达式结果”。该请求确实有效,但我想找到一种方法来摆脱警告。
I suppose I could store the connection in a variable but I don't really need it and I can't see the point of setting it to nil
the next line (although this would remove the warning)
我想我可以将连接存储在一个变量中,但我真的不需要它,而且我看不到将它设置到nil
下一行的意义(尽管这会消除警告)
Please note: I'm not 100% sure if it's Xcode 4 or the fact ARC is enabled.
请注意:我不是 100% 确定它是 Xcode 4 还是启用了 ARC。
回答by progrmr
When a function returns a result that you don't need you can cast it to void to eliminate the compiler warning:
当函数返回不需要的结果时,您可以将其强制转换为 void 以消除编译器警告:
(void) [[NSURLConnection alloc] initWithRequest:request delegate:self];
I haven't used ARC yet so I can't say if this is a good idea, before ARC you would need to keep this pointer result somewhere so you could release it.
我还没有使用 ARC,所以我不能说这是否是一个好主意,在 ARC 之前,您需要将此指针结果保存在某处以便您可以释放它。
回答by Elliot
progrmr's answer is correct, but here's an even cleaner way to do it:
progrmr 的答案是正确的,但这里有一种更简洁的方法:
[NSURLConnection connectionWithRequest:request delegate:self];
[NSURLConnection connectionWithRequest:request delegate:self];
This doesn't cause a warning, even if you don't cast the result to void.
这不会导致警告,即使您没有将结果强制转换为 void。
回答by thomas
Someone should be responsible for that NSURLConnection
. It is not needed to store the connection but it is better coding if you do. The problem is that after you created our NSURLConnection
no one has a pointer to that created instance which should not be the case.
有人应该为此负责NSURLConnection
。不需要存储连接,但如果你这样做,它是更好的编码。问题是,在您创建我们之后,NSURLConnection
没有人拥有指向该创建实例的指针,这不应该是这种情况。
Let's assume the following example:
让我们假设以下示例:
- your instance of
ClassA
is creating an instane ofNSURLConnection
- your instance of
ClassA
is beeing released and dealloced NSURLConnection
is still alive and will fire the delegate to your deallocated instance.
- 您的实例
ClassA
正在创建一个实例NSURLConnection
- 你的实例
ClassA
正在被释放和解除分配 NSURLConnection
仍然活着,并将委托给您的解除分配的实例。
To solve that problem you should store the instance of NSURLConnection
and should release that connection if your instance of ClassA
is being dealloced which results in deallocating the instance of NSURLConnection
as well.
要解决该问题,您应该存储 的实例,NSURLConnection
并且如果您的 实例ClassA
正在被解除分配,则应该释放该连接,这会导致也解除分配 的实例NSURLConnection
。