简体   繁体   English

Xcode 4警告“ NSURLConnection的表达结果未使用”

[英]Xcode 4 warning "Expression result unused” for NSURLConnection

I'm just trying to do my usual data transfert. 我只是在尝试进行通常的数据传输。 I define my NSMutableURLRequest then call 我定义我的NSMutableURLRequest然后调用

[[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. Xcode 3以前可以这样做,但Xcode 4会警告我该行上的“ 表达式结果未使用 ”。 The request does work but I would like to find a way to get rid of the warning. 该请求确实有效,但我想找到一种摆脱警告的方法。

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。

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之前,您需要将此指针结果保存在某个地方以便可以释放它。

progrmr's answer is correct, but here's an even cleaner way to do it: progrmr的答案是正确的,但这是一种更干净的方法:

[NSURLConnection connectionWithRequest:request delegate:self];

This doesn't cause a warning, even if you don't cast the result to void. 即使您不将结果强制无效,这也不会引起警告。

Someone should be responsible for that NSURLConnection . 有人应对该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没有人拥有指向所创建实例的指针,事实并非如此。

Let's assume the following example: 让我们假设以下示例:

  1. your instance of ClassA is creating an instane of NSURLConnection 您的ClassA实例正在创建NSURLConnection实例
  2. your instance of ClassA is beeing released and dealloced 您的ClassA实例正在释放并释放
  3. NSURLConnection is still alive and will fire the delegate to your deallocated instance. NSURLConnection仍然有效,并且将把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实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM